28

I have a WinForms application with a DataGridView, which DataSource is a DataTable (filled from SQL Server) which has a column of xxx. The following code raises the exception of

ArgumentException was unhandled. Column named xxx cannot be found.

foreach (DataGridViewRow row in Rows)
{
    if (object.Equals(row.Cells["xxx"].Value, 123))
}

Is it possible to get the cell values by column name?

Beetee
  • 475
  • 1
  • 7
  • 18
ca9163d9
  • 27,283
  • 64
  • 210
  • 413
  • If you set the column's "Name" property it does work. I stumbled about this because I had several grids on my form and did not yet set the column names on the grid I was just testing. :) – IngoB Jul 24 '23 at 11:24

8 Answers8

28

DataGridViewColumn objects have a Name (shown only in the forms designer) and a HeaderText (shown in the GUI at the top of the column) property. The indexer in your example uses the column's Name property, so since you say that isn't working I assume you're really trying to use the column's header.

There isn't anything built in that does what you want, but it's easy enough to add. I'd use an extension method to make it easy to use:

public static class DataGridHelper
{
    public static object GetCellValueFromColumnHeader(this DataGridViewCellCollection CellCollection, string HeaderText)
    {
        return CellCollection.Cast<DataGridViewCell>().First(c => c.OwningColumn.HeaderText == HeaderText).Value;            
    }
}

And then in your code:

foreach (DataGridViewRow row in Rows)
{
    if (object.Equals(row.Cells.GetCellValueFromColumnHeader("xxx"), 123))
    {
        // ...
    }
 }
Patrick Quirk
  • 23,334
  • 2
  • 57
  • 88
  • can simply get the `xxx` index in the helper and use it, right? – Katia Apr 30 '14 at 07:35
  • `private int indexOf( DataGridView dgv, string name) { return dgv.Columns[name].Index; } foreach (DataGridViewRow row in Rows) { if (object.Equals(row.Cells[indexOf(dgv, "xxx"].Value, 123)) { // ... } }` – Katia Apr 30 '14 at 07:42
  • 2
    I like this solution because it is super extensible! I needed to get the value with DataPropertyName because my header text was changed to make the table more readable. This helper function works straightforwardly, just need to replace HeaderText with DataPropertyName. – rgshenoy Oct 26 '15 at 10:12
  • 1
    how would this be in VB.NET? – intrixius Jul 06 '18 at 12:26
  • In VB the entry by Dominique Mathieu works: row.Cells(yourdataGridView.Columns("xxx").Index).Value – Tim Makins May 08 '21 at 17:31
14

By doing this you will be able to access the cell at the "xxx" column name for the currently selected row.

dataGridView1.SelectedRows[0].Cells["xxx"]
Redoman
  • 3,059
  • 3
  • 34
  • 62
13

Yes, just remove the quotes and add .Index, i.e.

foreach (DataGridViewRow row in Rows)
{
    if (object.Equals(row.Cells[xxx.Index].Value, 123)) 

...that is if your column is really called xxx and not some other name like Column1, etc. You can set the column name and the column header independantly so check in the designer.

Richard Hansell
  • 5,315
  • 1
  • 16
  • 35
6

I found this:

  1. Right mouse click on the datagridview.
  2. Select from popup menu "Edit columns".
  3. Select column that you want. Design/(Name) - it's what you were looking for.

Sample:

if(dataGridViewProjectList.Rows[dataGridViewProjectList.CurrentCell.RowIndex].Cells["dateendDataGridViewTextBoxColumn"].Value.ToString().Length == 0)
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Sergey
  • 61
  • 1
  • 1
3

if you grid column in design mode. then column name was gridView~~~Column controls's name example :

DataGridViewTextBoxColumn idDataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();

idDataGridViewTextBoxColumn.Name = "idDataGridViewTextBoxColumn";

then you can access right this, you want.

row.Cells["idDataGridViewTextBoxColumn"].Value
Kim Ki Won
  • 1,795
  • 1
  • 22
  • 22
2

If you look about :

row.Cells["xxx"].Value;

By this :

row.Cells[yourdataGridView.Columns["xxx"].Index].Value;
0

If your datagridviews columns are dynamic with dedicated headertext, then you can assign a name to that particular column of DGV like this:

dataGridView2.Columns[1].HeaderText = "Invoice No";
dataGridView2.Columns[1].Name = "invoice_no";

and then can use that name as an index like this :

foreach (DataGridViewRow row in rows_with_checked_column)
{
    MessageBox.Show(row.Cells["invoice_no"].Value.ToString());
}
Josef
  • 2,869
  • 2
  • 22
  • 23
0

You can also try it under the DataGridview cell click event

         TxtTankPainting.Text = DataGridTransfData.Rows[e.RowIndex].Cells["TankPainting"].Value.ToString();
                TxtTankPosition.Text = DataGridTransfData.Rows[e.RowIndex].Cells["TankPosition"].Value.ToString();
                TxtCorePosition.Text = DataGridTransfData.Rows[e.RowIndex].Cells["CorePosition"].Value.ToString();