methuselah
methuselah

Reputation: 13206

Get value of cell and transfer it to a second form

How would I get the value of a cell that is clicked on and transfer it onto a second form?

This is my code so far:

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 1)
        {
            var form2 = new Form2();
            this.Hide();
            form2.Show();
        }
    }

Upvotes: 0

Views: 46

Answers (2)

Edper
Edper

Reputation: 9322

Change the constructor of the form that would receive value, like:

  public Form2(String passCellValue)
    {
        InitializeComponent();
    }

Then call like this:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 1)
    {
        String cellValue;
        cellValue = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
        var form2 = new Form2(cellValue);
        this.Hide();
        form2.Show();
    }
}

Upvotes: 1

Ajay2707
Ajay2707

Reputation: 5798

Its very easy

private void gridAgentDetails_Click(object sender, EventArgs e) {  for (int i = 0; i < this.gridAgentDetails.CurrentRowIndex; i++)  {      string str = gridAgentDetails.Text; }}

plaese check link

How to get column value of grid in C# Windows application?

and to transfer data , you can make general property or variable. Please check this link.

http://social.msdn.microsoft.com/Forums/vstudio/en-US/08943b7b-9603-49ad-950c-62e8f8325df2/how-to-pass-value-data-grid-view-click-to-another-form?forum=vbgeneral

Upvotes: 0

Related Questions