gridview selected row value to textbox on another form
how to retrive value of selected row of gridview in textbox while gridview and textbox both r on different window form i.e. form1 and form2
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
siva kumarPosted Jan 5, 2016, 7:20 AM
VulpesPosted May 2, 2013, 2:24 PM
However, if you want to track the selected rows dynamically, then you'll need to use an event. Something like this (off top of my head so may be errors):
// Form2
private void add_item_Load(object sender, EventArgs e)
{
Item_setup f1 = (Item_setup)Application.OpenForms["Item_setup"];
f1.dataGridView1.SelectionChanged += Form1_DGV_SelectionChanged;
// call handler manually to get initial value
Form1_DGV_SelectionChanged(f1.dataGridView1, EventArgs.Empty);
}
// add this handler manually to Form2, not through VS designer
private void Form1_DGV_SelectionChanged(object sender, EventArgs e)
{
DataGridView dgv = (DataGridView)sender;
DataGridViewSelectedRowCollection selRows = dgv.SelectedRows;
if (selRows != null && selRows.Count > 0 && selRows[0].Cells[0].Value != null)
{
textBox1.Text = selRows[0].Cells[0].Value.ToString();
}
else
{
textBox1.Text = "";
}
}
sunny saharawatPosted May 2, 2013, 1:48 AM
sunny saharawatPosted May 2, 2013, 1:15 AM
VulpesPosted May 1, 2013, 9:53 AM
Then on Form2 use this code to get the SelectedRows collection:
Form1 f1 = (Form1)Application.OpenForms["Form1"];
DataGridViewSelectedRowCollection selRows = f1.dataGridView1.SelectedRows;
If the value you want is in the first column of the first (or only) selected row, you can then do:
textBox1.Text = selRows[0].Cells[0].Value.ToString();