如何获取数据表中列的最后一个单元格的id [英] how to get the last cell's id of a column in a data table

查看:85
本文介绍了如何获取数据表中列的最后一个单元格的id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取数据表中列的最后一个单元格的id

how to get the last cell's id of a column in a data table

推荐答案

请阅读我对该问题的评论并查看下面的示例(使用 Linq [ ^ ]):



Please, read my comment to the question and have a look at below sample (using Linq[^]):

DataTable dt = new DataTable();
DataColumn dc = new DataColumn("ID", System.Type.GetType("System.Int32"));
dt.Columns.Add(dc);
dc = new DataColumn("UserId", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
dc = new DataColumn("Password", System.Type.GetType("System.String"));
dt.Columns.Add(dc);

dt.Rows.Add(new Object[]{1, "vvv", "v"});
dt.Rows.Add(new Object[]{2, "asd", "a"});
//max id
dt.Rows.Add(new Object[]{4, "sdasd", "r"});
//last row ;)
dt.Rows.Add(new Object[]{3, "dsa", "r"});


int lastid = dt.AsEnumerable().Select(x=>x.Field<int>("Id")).Last();
//returns 3

int maxid = dt.AsEnumerable().Max(x=>x.Field<int>("Id"));
//returns 4





如您所见,最后一行并不意味着 Max(ID )

根据您想要达到的目的,根据需要更改代码;)



As you can see, last row does not mean Max(ID).
Depending on what you want to achieve, change the code to your needs ;)


var a = dt.Rows[dt.Rows.Count - 1];





从DataTable获取最后一行数据。



To get last row data From DataTable.


这篇关于如何获取数据表中列的最后一个单元格的id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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