C# – Creating Data Table – Retrieving data

Creating Data Table

DataTable dt = new DataTable();

//Define Columns
DataColumn Column1 = new DataColumn("Column 1", typeof(string));
DataColumn Column2 = new DataColumn("Column 2", typeof(string));
DataColumn Column3 = new DataColumn("Column 3", typeof(string));
DataColumn Column4 = new DataColumn("Column 4", typeof(string));

//Add columns to data table
dt.Columns.Add(Column1);
dt.Columns.Add(Column2);
dt.Columns.Add(Column3);
dt.Columns.Add(Column4);

Retrieving Data

foreach (DataRow dr in dt.Rows)
{
   string col1=dr[0].tostring();
   string col2=dr["Column2"].tostring();
}

Get/Set Cell Value

      int row=2;
      dt.Rows[row]["Column1"] = "SomeValue";
      string col1 = dt.Rows[row]["Column1"].tostring();
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s