[Reserved] in C # through the data row in the DataTable

During operation Datatable data variables in C # in, sometimes we need to get the data to traverse DataTable variable values ​​for each row, for example, when the DataTable variable into List set of variables we will traverse the DataTable, DataTable variable traverse to get every line DataRow object, we can get on to the property values, etc. should all columns by rows of DataRow objects.

First, an example of a given type variable DataTable dataDt, the data includes two data list, a type string Name column, one of the Id column of integer type Int. It is defined as follows:

(1) for loop iterates DataTable object

The number of rows to get the entire DataTable table by the Count property of the DataTable Rows property variable variables, namely dataDt.Rows.Count.

  int rowCount = dataDt.Rows.Count;
  for (int index= 0; index < rowCount; index++)
   {
        string Name = dataDt.Rows[index]["Name"].ToString();
        int Id = Convert.ToInt32(dataDt.Rows[index]["Id"]);

         index = index + 1;
 }

(2) to traverse the object through the foreach loop DataTable DataRow objects

    foreach (DataRow dataRow in dataDt.Rows)
    {
         string Name = dataRow["Name"].ToString();
         int Id = Convert.ToInt32(dataRow["Id"]);
  }

  

Note: The text reproduced from personal bloggers station IT technology small fun house , the original link is in C # through the data line _IT technology DataTable little interest in the house .

Bloggers personal technology exchange group: 960 640 092, blogger micro-channel public numbers are as follows:

Guess you like

Origin www.cnblogs.com/xu-yi/p/11246377.html