我想遍历数据表中特定列的值?任何人都可以请给C#编码?如何遍历数据表中特定列的值?
回答
嗨试试下面的代码片段
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data;namespace ConsoleApplication1 { class Program { public static DataTable GetDataTable() { //Create a new DataTable object DataTable objDataTable = new DataTable(); //Create three columns with string as their type objDataTable.Columns.Add("Column 1", typeof(string)); objDataTable.Columns.Add("Column 2", typeof(string)); objDataTable.Columns.Add("Column 3", typeof(string)); //Adding some data in the rows of this DataTable objDataTable.Rows.Add(new string[] { "Row1 - Column1", "Row1 - Column2", "Row1 - Column3" }); objDataTable.Rows.Add(new string[] { "Row2 - Column1", "Row2 - Column2", "Row2 - Column3" }); objDataTable.Rows.Add(new string[] { "Row3 - Column1", "Row3 - Column2", "Row3 - Column3" }); return objDataTable;
} static void Main(string[] args) { foreach (DataRow row in GetDataTable().Rows) { object cellData = row["Column 1"]; Console.WriteLine(cellData); } } }
}
尝试下面基于Linq的解决方案
var values = (from t in dt.AsEnumarable() // dt is your data table
select t[ColumnName]).ToList().ForEach(your expression);
or try normal way
foreach(DataRow drow in dt.Rows)
{
string value = drow[columnname].ToString();
}
DataTable tbl = new DataTable();
foreach (DataRow row in tbl.Rows)
{
object cellData = row["colName"];
}
- 1. 遍历数据表中特定列的行
- 2. 如何遍历Excel工作表只提取特定列中的数据
- 3. 遍历列表元素,以获得特定的数据
- 4. 如何遍历数据表中的行?
- 5. 如何通过hashmap值遍历列表
- 6. 遍历数列表
- 7. 如何遍历ID列表
- 8. 如何遍历散列表数据列表
- 9. 如何遍历javascript中的列表
- 10. 遍历列中的值
- 11. 如何从Java中的特定键值开始遍历HashMap?
- 12. 遍历特定JSON
- 13. 如何遍历字节数组列表...?
- 14. 遍历列表
- 15. 遍历列表
- 16. 遍历数据表并删除列表中的列
- 17. 遍历列表的列表?
- 18. 遍历javascript中的列表
- 19. 如何从表头中遍历列vba
- 20. 遍历listview的特定ID?
- 21. 遍历会议寻找特定值
- 22. Iterartor - 如何遍历特定记录
- 23. 如何遍历特定工作簿?
- 24. 如何遍历数组以查询Mongo中的特定键?
- 25. 遍历R中的函数列表
- 26. 如何将值附加到列表然后遍历列表?
- 27. 如何遍历列表视图列
- 28. 遍历pandas中的数据帧数组并绘制列值
- 29. 如何遍历数据库表以在datagrid中添加数据?
- 30. 如何遍历thymeleaf中另一个列表中的列表?