for循环用于来自数据表od数据集的特定列的值 [英] for loop for values of a particular column from a datatable od Dataset
问题描述
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds, FileName);
int countbtn = ds.Tables [ 文件名跨度>] Rows.Count。
foreach (DataRow row in ds.Tables [ FileName]。行)
{
foreach (DataColumn dc in ds.Tables [ FileName]。列)
{
GenrateBtn(countbtn,row.ToString());
}
}
但是它在我的数据表中的所有列。
我希望第3列的值传递方法;
foreach (DataRow row in ds.Tables [ FileName]。行)
{
GenrateBtn(countbtn,row.Field< string>( 2 跨度>));
}
命名或索引你想要的列。
看看我对你的代码的评论如下:
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds, FileName);
int countbtn = ds.Tables [ 文件名跨度>] Rows.Count。
// 循环返回每行
foreach (DataRow row in ds.Tables [ < span class =code-string> FileName]。行)
{
// 遍历每一列
foreach (DataColumn dc in ds .Tables [ FileName]。列)
{
// 传递计数和整行。
GenrateBtn(countbtn,row.ToString());
}
}
以下是我的意思:
< pre lang =c#> SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds, FileName);
int countbtn = ds.Tables [ 文件名跨度>] Rows.Count。
// 循环返回每行
foreach (DataRow row in ds.Tables [ < span class =code-string> FileName]。行)
{
if ( false )
{
// 以下代码仍然是错了,但它利用了第二个循环
// 循环通过每列
foreach (DataColumn dc in ds.Tables [ FileName]。列)
{
// 传递计数和每列行。
GenrateBtn(countbtn,row [dc.Name] .ToString());
}
}
其他 {
// 这符合您的要求:
// 通过每一行的第三列(零索引)
GenrateBtn(countbtn,row [ 2 ]。ToString()) ;
// 或使用列名
GenrateBtn(countbtn ,row [ ThirdColumn]。ToString());
}
}
希望有所帮助
Andy
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds,"FileName");
int countbtn = ds.Tables["FileName"].Rows.Count;
foreach (DataRow row in ds.Tables["FileName"].Rows)
{
foreach (DataColumn dc in ds.Tables["FileName"].Columns)
{
GenrateBtn(countbtn, row.ToString());
}
}
But It goes all column in my data table.
I want values from 3rd column to pass method;
foreach(DataRow row in ds.Tables["FileName"].Rows) { GenrateBtn(countbtn,row.Field<string>(2)); }
Either name or index the column you want.
Look at my comments on your code below:
SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); sda.Fill(ds,"FileName"); int countbtn = ds.Tables["FileName"].Rows.Count; //loop through each row returned foreach (DataRow row in ds.Tables["FileName"].Rows) { //Loop through each column foreach (DataColumn dc in ds.Tables["FileName"].Columns) { //Pass the count and the whole row. GenrateBtn(countbtn, row.ToString()); } }
Below is what I think you meant:
SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); sda.Fill(ds,"FileName"); int countbtn = ds.Tables["FileName"].Rows.Count; //loop through each row returned foreach (DataRow row in ds.Tables["FileName"].Rows) { if(false) { //The below code is still wrong, but it makes use of the second loop //Loop through each column foreach (DataColumn dc in ds.Tables["FileName"].Columns) { //Pass the count and each column in the row. GenrateBtn(countbtn, row[dc.Name].ToString()); } } else{ //This follows your requirements: //Pass through the third column for each row (zero indexed) GenrateBtn(countbtn, row[2].ToString()); //OR use the column name GenrateBtn(countbtn, row["ThirdColumn"].ToString()); } }
Hope that helps
Andy
这篇关于for循环用于来自数据表od数据集的特定列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!