Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datatable from which i have to iterate through each row and and read the values of a particular column in which i have to give a condition.

can anyone help??
Posted

Use your coding with foreach.

as mentioned below.

foreach(DataRow row in DataTable.Rows)<br />
{<br />
//get the values here<br />
}<br />
 
Share this answer
 
foreach(DataRow row in DataTable.Rows)
{
//get the values here
}
 
Share this answer
 
Well, it depends on the way datatable is formed. Better if you have column names defined. Once there, it would be something like:
C#
string currentCellValue = string.Empty;

foreach(DataRow dr in myTable.Rows)
{
   // Here you get access to values at cell level.
   // Place your desired logic here.
 
   currentCellValue = dr["myColumnName"].ToString();
   // In case column names are not defined then
   // Assuming you need 3rd column value then
   currentCellValue = dr[2].ToString();
}
 
Share this answer
 
Well there are two ways to loop through the data(s). Whether you want to loop through connected or through the disconnected way. If I'm not mistaken the above answers by our members are disconnected way(correct me if wrong).

Below example is the way to loop through the database while connected:

try
{
      conn.Open();

      string commString = "SELECT * FROM database_table";

      SqlCommand comm = new SqlCommand(commString, conn);
      SqlDataReader reader = null;

      reader = comm.ExecuteReader();

      while (reader.Read())
      {
          if (reader["myID"].ToString() == textSearch.Text)
          {
              textID.Text = reader["myID"].ToString();
              textName.Text = reader["myName"].ToString();
              textIC.Text = reader["myIC"].ToString();
              textAddress.Text = reader["myAddress"].ToString();
              textContact.Text = reader["myContact"].ToString();
          }
      }
  }
  finally
  {
      conn.Close();
  }


myID,myName... are the columns for each row in the table
 
Share this answer
 
var test = (from obj in dt.AsEnumerable()
select new
{
test = obj["FirstName"].ToString()
}).ToList();
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 9-Dec-13 5:09am    
posting answer after 3 years..
but yyyyyyyyyyyyyyyy ???

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