vb.net 数据集中特定列的所有行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16400559/
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
All rows of specific column in dataset
提问by phadaphunk
I have a DataSet that looks like this :
我有一个如下所示的数据集:
| A | B | C | D | E | F | G | H | I | ... | Z |
--------------------------------------------------
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | ... | 26 |
|11 |22 |33 |44 |55 |66 |77 |88 |99 | ... | 2626 |
|111|222|333|444|555|666|777|888|999| ... |262626|
Values not related. I just have a lotof columns.
值不相关。我只有很多列。
I would like to go through all rows for a specific column.
Is it possible without going though all columns ? Because right now the only thing I can think of is this (let's say I want all rows for column D)
我想查看特定列的所有行。
是否可以不通过所有列?因为现在我唯一能想到的就是这个(假设我想要 D 列的所有行)
C#
C#
foreach(DataRow row in myDataSet.Tables(0).Rows)
if(row.Column == myDataSet.Tables(0).Columns("D"))
MessageBox.Show("I'm in Column B");
VB
VB
For Each row As DataRow In myDataSet.Tables(0).Rows
If row.Column Is myDataSet.Tables(0).Columns("D") Then
MessageBox.Show("I'm in Column B")
End If
Next
But this would loop through all columns. I would like to use a collection like myDataSet.Tables(0).Columns("D").Rowsbut it does not exist.
但这会遍历所有列。我想使用类似的集合,myDataSet.Tables(0).Columns("D").Rows但它不存在。
回答by Tim Schmelter
DataRowhas an indexerthat you can use:
DataRow有一个可以使用的索引器:
foreach(DataRow row in myDataSet.Tables[0].Rows)
Console.WriteLine("I'm in Column B: " + row["D"]);
You can access it via nameor ordinal-indexof the field. The third overloadcan be used if you have a reference of DataColumnand is used by the others after the column was found. If you don't want to "search" for the column(although the effort is negligible) use this:
您可以通过字段的名称或序数索引来访问它。在第三个重载,如果你有一个参考,可以使用 DataColumn和使用于其他列后发现。如果您不想“搜索”该列(尽管工作量可以忽略不计),请使用以下命令:
DataColumn col = myDataSet.Tables[0].Columns["D"];
foreach(DataRow row in myDataSet.Tables[0].Rows)
Console.WriteLine("I'm in Column B: " + row[col]);
But you could also use Linq, for example if you want to sum all values in this column:
但是您也可以使用 Linq,例如,如果您想对这一列中的所有值求和:
int dTotal = myDataSet.Tables[0].AsEnumerable().Sum(r => r.Field<int>("D"));
回答by Steve
In an immaginary grid you scroll the rows vertically and access the column horizontally
在虚拟网格中,您可以垂直滚动行并水平访问列
foreach(DataRow row in myDataSet.Tables(0).Rows)
{
// At this point the row iterator point to a row
// where all the values in the schema columns are available
// Indexing with the column name will result in related value
MessageBox.Show(row["D"].ToString();
}

