Rok
Rok

Reputation: 15

C# Get Value of DataGridView

I got a problem with 2 DataGrid Views. Both are the same. the only different is, that the second one has a filter to sort out the closed cases.

So there are: faelleDataGridView faelleDataGridView2

All I want is to select the value of the column "ID" of the selected row. After that it will open a new form were I set the varaible for the new filter (I only want to see entries whitch fit to the ID).

This will start after i have made a double click on the cell.

This is my source code:

        private void faelleDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    { // This one is working with no errors

        if (e.RowIndex >= 0)
        {
            FallDetail fFallDetail = new FallDetail();
            DataGridViewRow row = faelleDataGridView.Rows[e.RowIndex];
            //MessageBox.Show(row.Cells["ID"].Value.ToString());
            fFallDetail.fFilter = row.Cells["ID"].Value.ToString();
            fFallDetail.Show();
        }

    }

    private void faelleDataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    { // This one will result an error
        if (e.RowIndex >= 0)
        {
            FallDetail fFallDetail = new FallDetail();
            DataGridViewRow row = faelleDataGridView1.Rows[e.RowIndex];
            //MessageBox.Show(row.Cells["ID"].Value.ToString());
            fFallDetail.fFilter = row.Cells["ID"].Value.ToString();
            fFallDetail.Show();
        }
    }

After I have started the second one I got an error. It says, that he can not find the cell named "ID".

The Error (translated from german): An exception error of the type "System.ArgumentException" is occurred at System.Windows.Forms.dll.

Additional Information: The column ID could not be found.

I have checked The ID column is only visible=false. But there is the same at the first datagridview.

Do you have any idea how I can solve this problem or what I'm doing wrong?

Thank you in advance.

Fixed the Problem by using the correct name in the properties

Greez Rok

Upvotes: 0

Views: 1668

Answers (1)

Richard
Richard

Reputation: 588

When you search for index fFallDetail.fFilter = row.Cells ["ID"]. Value.ToString (); which is put the name of the column, check that each column is the appropriate name. One thing is the header and another thing is the name. You can check it from the properties in the GridView designer.

Upvotes: 2

Related Questions