Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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;
Posted
Updated 19-Aug-15 22:04pm
v2

C#
foreach(DataRow row in ds.Tables["FileName"].Rows)
{
   GenrateBtn(countbtn,row.Field<string>(2));
}
 
Share this answer
 
Either name or index the column you want.

Look at my comments on your code below:
C#
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:
C#
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900