简体   繁体   English

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

[英]Retrieve GridView row based on unique cell value

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. 例如,我有一个包含4列(名称,m1,m2,m3)的网格视图,并且名称包含唯一值。 So, I want to get the grid view row corresponding to the name specified. 因此,我想获取与指定名称相对应的网格视图行。

Thanks 谢谢

EDIT: I realized you probably meant the ASP.NET GridView not the WinForm DataGridView, which is what I originally answered for. 编辑:我意识到您可能是指ASP.NET GridView而不是WinForm DataGridView,这是我最初的回答。 The approach is very different in that case. 在这种情况下,方法是非常不同的。

Just in case I left the WinForm DataGridView approach below. 以防万一我在下面离开了WinForm DataGridView方法。

ASP.NET GridView ASP.NET GridView

The GridView is somewhat annoying in that it doesn't allow you to access cells by column name. GridView有点令人讨厌,因为它不允许您按列名访问单元格。 Instead you need to know the index. 相反,您需要知道索引。 You could hardcode it, but that's not desirable. 您可以对其进行硬编码,但这不是可取的。

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 DataGridView

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

This is what the DataKey property is used for. 这就是DataKey属性的用途。 So: GridView1.DataKeyNames="name" 因此: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. 现在,如果不想,则无需显示“名称”列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM