本文介绍了如何在数据表中获取DataRow索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好.
我的代码中有一个绑定到dataGridview的DataTable.
当我从数据库中获取数据时,gridview可以正常工作.它充满了数据.
我要做的是获取指定行的索引(实际上是主键的值).
我该怎么办?
最好的问候...
Hey everyone.
I have a DataTable which is bind to dataGridview in my code.
When I get datas from database, the gridview works fine. it fills with data.
What I want to do is getting the index(actually the value of the Primary key) of the of the specified row.
How can I do it?
Me best regards...
推荐答案
public static int Getindex(string row,DataTable dt)
{
int id=0;
string rowvalue = row;
try
{
id = (from DataRow dr in dt.Rows
where (string)dr["Description"] == rowvalue
select (int)dr["Id"]).FirstOrDefault();
}
catch(Exception ex)
{
//Response.Write(ex.Message);
}
return id;
}
此linq查询可能会解决您的问题,
希望这会有所帮助,
--G-
this linq query might solve your problem,
hope this helps,
--G--
这篇关于如何在数据表中获取DataRow索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!