从视图状态到列表获取数据(视图状态具有表)

编程入门 行业动态 更新时间:2024-10-24 16:25:11
本文介绍了从视图状态到列表获取数据(视图状态具有表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我在一个像下面这样的列表中有一个数据,现在我想从已经包含表的viewstate中获取数据,我想要一列.我该怎么办?

Suppose I have a data in a list like below and now I want to get data from viewstate which already contain table in that I want one column. How can I do that?

public List<Employee> GetEmployeeList() { List<Employee> empList = new List<Employee>(); empList.Add(new Employee() { ID = 1, Email = "Mary@somemail"}); empList.Add(new Employee() { ID = 2, Email = "John@somemail"}); empList.Add(new Employee() { ID = 3, Email = "Amber@somemail"}); empList.Add(new Employee() { ID = 8, Email = "Jonay@somemail"}); empList.Add(new Employee() { ID = 9, Email = "Robert@somemail"}); empList.Add(new Employee() { ID = 10, Email = "Krishna@somemail" }); return empList; }

推荐答案

如果我正确地回答了您的问题,则说明viewstate中有一个数据表,并且您想添加您上面显示的列表中的特定列.如果是这样,您可以: 1-从viewstate获取数据表. DataTable dataTable = ViewState["myDataTable"] 2-从GetEmployeeList(...)获取列表. List<Employee> listEmployee = GetEmployeeList(); 3-遍历数据表的行并将值添加到列表变量. If i''m getting your question right you have a datatable in the viewstate and you want to add all the values from rows of a particular column to the list you have shown above. if that is the case you can: 1- Get Datatable from viewstate. DataTable dataTable = ViewState["myDataTable"] 2- Get the list from GetEmployeeList(...). List<Employee> listEmployee = GetEmployeeList(); 3- Iterate through rows of datatable and add values to your list variable. for(rowIndex=0; rowIndex<datatable.rows.count;>{ string columnValue = dataTable.Rows[rowIndex]["email"]; Employee emp = new Employee(){ID=someID, Email=columnValue}); }

希望对您有所帮助. 谢谢, Hemant

Hope it will help. thanks, Hemant

更多推荐

从视图状态到列表获取数据(视图状态具有表)

本文发布于:2023-10-18 12:51:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504278.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   状态   数据   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!