C# 如何通过列名获取DataGridView的单元格值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13436553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:32:48  来源:igfitidea点击:

How to get cell value of DataGridView by column name?

c#winformsdatagridviewcolumn

提问by ca9163d9

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

我有一个WinForms应用程序DataGridView,其数据源是具有一列数据表(从SQL Server填写)xxx。以下代码引发了异常

ArgumentException was unhandled. Column named xxx cannot be found.

ArgumentException 未处理。找不到名为 xxx 的列。

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

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

是否可以通过列名获取单元格值?

采纳答案by Patrick Quirk

DataGridViewColumnobjects 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 Nameproperty, so since you say that isn't working I assume you're really trying to use the column's header.

DataGridViewColumn对象具有Name(仅在表单设计器中显示)和HeaderText(在列顶部的 GUI 中显示)属性。您示例中的索引器使用列的Name属性,因此既然您说这不起作用,我假设您确实在尝试使用列的标题。

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

回答by Sergey

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.
  1. 鼠标右键单击数据网格视图。
  2. 从弹出菜单中选择“编辑列”。
  3. 选择所需的列。设计/(名称) - 这就是你要找的。

Sample:

样本:

if(dataGridViewProjectList.Rows[dataGridViewProjectList.CurrentCell.RowIndex].Cells["dateendDataGridViewTextBoxColumn"].Value.ToString().Length == 0)

回答by Richard Hansell

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

是的,只需删除引号并添加 .Index,即

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.

...也就是说,如果您的列确实被称为 xxx 而不是诸如 Column1 等其他名称。您可以独立设置列名和列标题,以便在设计器中检查。

回答by Kim Ki Won

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

如果您在设计模式下对列进行网格化。那么列名称是 gridView~~~列控件的名称示例:

DataGridViewTextBoxColumn idDataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();

idDataGridViewTextBoxColumn.Name = "idDataGridViewTextBoxColumn";

then you can access right this, you want.

然后你可以访问这个,你想要的。

row.Cells["idDataGridViewTextBoxColumn"].Value

回答by jj_

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

通过这样做,您将能够访问当前选定行的“xxx”列名称处的单元格。

dataGridView1.SelectedRows[0].Cells["xxx"]

回答by Dominique Mathieu

If you look about :

如果你看看:

row.Cells["xxx"].Value;

By this :

这样 :

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