如何在数据表中获取DataRow索引 [英] How to get DataRow index in a Datatable

查看:666
本文介绍了如何在数据表中获取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--


设置数据键名以选择特定的行格式DB ...
通过此链接获取更多详细信息,以进行添加和编辑.


gridview-datakey-value
编辑更新
Set datakeyname to select particular row form DB...
GO THRU THIS LINK FOR MORE DETAILS ON ADD AND EDIT..


gridview-datakey-value
Edit_update


这篇关于如何在数据表中获取DataRow索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆