Column name DataGridView not found

Why am I getting an exception that the column name was not found for MyEntity, as well as for FullName columns? Although I see that the column names are displayed in the user interface.

InitializeComponent();

dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
    int rowIndex = dataGridView1.Rows.IndexOf(row);
    myObject = dataGridView1.Rows[rowIndex].Cells["MyEntity"].Value as IEntityObject;
    fileName = dataGridView1.Rows[rowIndex].Cells["FullName"].Value.ToString();       
}
+4
source share
2 answers

Because, oddly enough, this datagridview column is not actually named the same as the DataTable column name. If you look at the collection of columns in the constructor properties window, you will see that this is probably called something like "DataGridViewColumn4" or similar.

If you know the index, you should use it or rename the columns to DT column names.

+11
source

, datagridview :

        DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn();
        col.Width = 90;
        col.HeaderText = "Sync To Vend?"; //Header cell text
        col.Name = "SyncVendBox"; //This is the important part
        ProductGrid.Columns.Insert(2, col);

:

        for (int i = 0; i < ProductGrid.Rows.Count; i++)
        {
            ProductGrid.Rows[i].Cells["SyncVendBox"].Value = true;
        }
0

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


All Articles