C# 显示数据表的数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14376828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Displaying data of data table
提问by Arianule
I want to display values or write them to a text file that it is written as such from a data set
我想显示值或将它们写入一个文本文件,它是从数据集中写入的
ColumnID columnName ColumnFamilyName
ValueOne ValueTwo ValueThree
ValueThree ValueFour ValueFive
ValueSix ValueSeven ValueEight
I have done this which does not do the trick
我已经完成了这并不能解决问题
foreach (DataRow row in myTopTenData.Rows)
{
Console.WriteLine();
foreach (DataColumn col in myTopTenData.Columns)
{
Console.Write(row[0].ToString() + " ");
}
}
How can I do this?
我怎样才能做到这一点?
采纳答案by noobob
I still can't post a comment but here is a quick answer:
我仍然无法发表评论,但这里有一个快速答案:
foreach(DataRow row in myTopTenData.Rows)
{
string ID = row["ColumnID"].ToString();
string Name= row["columnName"].ToString();
string FamilyName= row["ColumnFamilyName"].ToString();
}
Make sure to check for null
values when retrieving the data
确保null
在检索数据时检查值
回答by Steve
Assuming that myTopTenData is a DataTable then you loop on rows of a datatable in this way
假设 myTopTenData 是一个数据表,那么你以这种方式循环数据表的行
foreach (DataRow row in myTopTenData.Rows)
{
Console.WriteLine();
for(int x = 0; x < myTopTenData.Columns.Count; x++)
{
Console.Write(row[x].ToString() + " ");
}
}
Of course this snippet should be taken only as a trivial example.
You need to consider null values and a robust error checking.
当然,这个片段应该只作为一个简单的例子。
您需要考虑空值和强大的错误检查。
回答by Chama
you can use Datagrid to display your DataTable like this :
您可以使用 Datagrid 像这样显示您的 DataTable:
in Wpf :
在 WPF 中:
datagrid.SetBinding(ItemsControl.ItemsSourceProperty, new System.Windows.Data.Binding { Source = dt});
========================================================================
================================================== ======================
in winform :
在winform中:
datagrid.DataSource(datatable);