I want to get the specific column value of the current row of a datagrid type in a text box of another form. How can I do it. I use c sharp

I want to pass the column value of the current row of a datagridview. I have a button in one column in a datagridview. When I click on the button, another form opens. I have a text box on another form. Therefore, I want to get the column value of the current row in a text field of another form. I am using c sharp.

Any help is appreciated.

+1
source share
1 answer

Here is an example using the constructor to get the value:

public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //test data this.dataGridView1.Rows.Add(1); this.dataGridView1[1, 0].Value = "testValue1"; this.dataGridView1[1, 1].Value = "testValue2"; } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { //Button is in column 0. if ((e.ColumnIndex != 0) || (e.RowIndex < 0)) { return; } string valueToPass = (dataGridView1[1, e.RowIndex].Value as string) ?? String.Empty; Form2 f2 = new Form2(valueToPass); f2.Show(); } } public partial class Form2 : Form { public Form2(string valueFromOtherForm) { InitializeComponent(); this.textBox1.Text = valueFromOtherForm; } } 
+1
source

Source: https://habr.com/ru/post/1304523/


All Articles