查询 DataGrid 中某列数据 ,并返回查询结果的行索引

来源:百度知道 编辑:UC知道 时间:2024/05/19 00:10:22
其中DataGrid用DataTable数据源绑定,要实现可以查询DataGrid某列的数据,返回查询结果的行索引。
若能查询绑定其数据源的数据及索引也可以。
补充:是WinForm ,而不是WebForm

for(int i=0;i< datagrid.rows.count;i++)
if(datagrid.rows.cell[i][M].tostring.equals(查询比较对象)) //M是要查询的第M+1列
{
return i;
也可以return datagrid.rows.cells[i][K]--这个是本行的关键标示,能够确定一条记录
i=gridview.rows.count;
}

DataTable dt = new DataTable();
DataRow[] drs = dt.Select(string.Format("查询列='{0}'", "查询值"));
if (drs.Length > 0)
{
int 行号 = dt.Rows.IndexOf(drs[0]);
}

问题理解的不是很清楚。
要是获取DataGrid某单元格的内容,直接索引就行了。
要是查询某个值,用循环,如qiubit回答的(第一种情况,“查询DataGrid某列的数据”),
如 syler回答的( 第二种情况,“查询绑定其数据源的数据及索引”)
哈哈

For(Int i=0;i< DataGrid1.Rows.Count;i++)
If(DataGrid1.Rows.Cell[i]["查询列名"或列号+1].ToString().Equals(要查询的值))
{
Return i;
}

datarow[] dr =dt(绑定的表)。select(条件)
得到符合条件的数据行集合
datatable dtnew=new datatable();
for 循环 把需要的列加到新表 dtnew 里?