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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
VulpesRank #55 · 29.2K reputationPosted May 1, 2013, 5:53 AM EDT
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();
sunny saharawat11 reputationPosted May 1, 2013, 9:15 PM EDT
sunny saharawat11 reputationPosted May 1, 2013, 9:48 PM EDT
VulpesRank #55 · 29.2K reputationPosted May 2, 2013, 10:24 AM EDT
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 = "";
}
}
siva kumar2 reputationPosted Jan 5, 2016, 2:20 AM EST