سؤال

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?

هل كانت مفيدة؟

المحلول

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))
    {
        // ...
    }
 }

نصائح أخرى

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.

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"]

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)

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

If you look about :

row.Cells["xxx"].Value;

By this :

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

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());
}

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();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top