C# 根据唯一单元格值检索 GridView 行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1099102/
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-06 08:11:55  来源:igfitidea点击:

Retrieve GridView row based on unique cell value

c#.netasp.netgridviewfilter

提问by

I want to retrieve the grid view row based on the cell value. For example, I am having a grid view with 4 columns (name,m1,m2,m3) and name contains unique values. So, I want to get the grid view row corresponding to the name specified.

我想根据单元格值检索网格视图行。例如,我有一个包含 4 列(名称、m1、m2、m3)的网格视图,名称包含唯一值。所以,我想获取与指定名称对应的网格视图行。

Thanks

谢谢

回答by Ahmad Mageed

EDIT: I realized you probably meant the ASP.NET GridView not the WinForm DataGridView, which is what I originally answered for. The approach is very different in that case.

编辑:我意识到你可能指的是 ASP.NET GridView 而不是 WinForm DataGridView,这是我最初的回答。在这种情况下,方法非常不同。

Just in case I left the WinForm DataGridView approach below.

以防万一我离开了下面的 WinForm DataGridView 方法。

ASP.NET GridView

ASP.NET 网格视图

The GridView is somewhat annoying in that it doesn't allow you to access cells by column name. Instead you need to know the index. You could hardcode it, but that's not desirable.

GridView 有点烦人,因为它不允许您按列名访问单元格。相反,您需要知道索引。您可以对其进行硬编码,但这是不可取的。

Hardcoded approach:

硬编码方法:

string searchValue = "SpecifiedName";
// where 1 is the hardcoded cell index
var query = from GridViewRow row in GridView1.Rows
            where row.Cells[1].Text == searchValue
            select row;
GridViewRow result = query.FirstOrDefault();

Dynamic Approach (Column Index Lookup):

动态方法(列索引查找):

string colName = "name";
int index = (from DataControlField col in GridView1.Columns
            where col.HeaderText == colName
            select GridView1.Columns.IndexOf(col)).FirstOrDefault();

// index used
var query = from GridViewRow row in GridView1.Rows
        where row.Cells[index].Text == searchValue
        select row;
GridViewRow result = query.FirstOrDefault();

Alternate index lookup:instead of using HeaderText you can use BoundField.

备用索引查找:您可以使用 BoundField,而不是使用 HeaderText。

int index = (from DataControlField col in GridView1.Columns
            where ((BoundField)col).DataField == colName
            select GridView1.Columns.IndexOf(col)).FirstOrDefault();

WinForm DataGridView

WinForm 数据网格视图

Kept this here just in case.

把这个留在这里以防万一。

string name = "SpecifiedName";
var query = from DataGridViewRow row in dataGridView1.Rows
            where row.Cells["name"].Value.ToString() == name
            select row;
// the row will be returned by this or contain a default value if not found
DataGridViewRow result = query.FirstOrDefault();

回答by JBrooks

This is what the DataKey property is used for. So: GridView1.DataKeyNames="name"

这就是 DataKey 属性的用途。所以:GridView1.DataKeyNames="name"

And to find your match:

并找到您的匹配项:

   foreach (GridViewRow gvr in GridView1.Rows)
    {
        if (GridView1.DataKeys[gvr.RowIndex].ToString().Equals("mymatch"))
        {
            GridView1.SelectedIndex = gvr.RowIndex;
            break;
        }
    }     

More code then needed to do this, but you get the idea. Now you don't need to show the "name" column if you don't want to.

然后需要更多的代码来做到这一点,但你明白了。现在,如果您不想显示“名称”列,则无需显示。