views:

63

answers:

1

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

Any help is appreciated.

A: 

Here is an example using the constructor to receive 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;
    }

}
thedugas
Thanks . Appreciate it