wpf 循环遍历数据集表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15693536/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:22:17  来源:igfitidea点击:

Looping through DataSet table

c#wpfdatagridvisual-studio-2012wpfdatagrid

提问by touyets

I have installed VS 2012 after many years using VS 2010 and I can't even manage to loop through table rows anymore (which was a doddle before) and it is absolutely critical to my project.

在使用 VS 2010 多年后,我已经安装了 VS 2012,我什至无法再循环遍历表行(之前这是一个小问题),这对我的项目绝对至关重要。

I tried many ways of calling DataGrid.Rowsbut that is no longer available to users at all, so I am trying to find a work around.

我尝试了多种呼叫方式,DataGrid.Rows但用户根本无法使用这种方式,因此我正在努力寻找解决方法。

Here's and here is what i have come up with , but the second part is just not working for me.

这是我想出的,但第二部分对我不起作用。

foreach (TableRow row in MyDatabaseDataSet.Tables["Table1"].Rows)
{
//Pesky lengthy non-working code supposed to generate 
//controls dependent on row cell value
}

Just to make things simpler (the code not working is just so lengthy it would be counter-constructive to post it here. Instead let's try and get a messagebox to show the value of the cell in the 2nd column of the row looked at.

只是为了让事情更简单(不工作的代码太长了,把它张贴在这里是有违建设性的。相反,让我们尝试获取一个消息框来显示所查看行的第二列中单元格的值。

But nothing does it.

但没有什么。

i tried many many ways that i could think of including:

我尝试了很多我能想到的方法,包括:

MessageBox.Show(row.Cells[2].Value);
MessageBox.Show(row.Cells[2].Text);

But it just will not work for me at all!

但这对我根本不起作用!

Doesn't even want to run the project at all.

甚至根本不想运行该项目。

How can I loop through all the rows in a DataTableor even a Datagrid?

如何遍历 aDataTable甚至 a中的所有行Datagrid

PS: I forgot to mention but I also added the Load Data segment in the code prior to the foreach statement written above.

PS:我忘了提,但我还在上面写的 foreach 语句之前的代码中添加了 Load Data 段。

回答by Tim Schmelter

Maybe i'm overlooking something simple, but DataTable.Rowsreturns a DataRowCollectionwhich contains DataRowsnot TableRows.

也许我可以俯瞰简单的东西,而是DataTable.Rows返回一个DataRowCollection包含DataRows没有TableRows

So instead of

所以代替

foreach (TableRow row in MyDatabaseDataSet.Tables["Table1"].Rows)
{
    // ...
}

this:

这个:

foreach (DataRow row in MyDatabaseDataSet.Tables["Table1"].Rows)
{
    // ...
}

回答by Moho

DataTable.Rowsreturns an instance of DataRowCollection, which contains DataRowobjects, not TableRow

DataTable.Rows返回 的实例DataRowCollection,其中包含DataRow对象,而不是TableRow

foreach( DataRow row in MyDatabaseDataSet.Tables["Table1"].Rows)
{
   // code here
}