DataTable in C#

Here is the sample code for creating and assigning values in to a datatable and set to datagrid via coding….

DataTable dt = new DataTable();

dt.Columns.Add("S.No", typeof(string));

dt.Columns.Add("UserID",typeof(string));

dt.Columns.Add("FamilyId",typeof(string));

dt.Columns.Add("PhotoGalleryId",typeof(string));

dt.Columns.Add("FileName",typeof(string));

dt.Columns.Add("Status", typeof(string));

dt.Columns.Add("Success", typeof(string));

dt.Columns.Add("ImagePath", typeof(string));

DataRow Dr;

command=new SqlCommand("select * from [Assets]",connection);

connection.Open();

SqlDataReader reader = command.ExecuteReader();

int i = 1;

while(reader.Read())

{

Dr=dt.NewRow();

Dr[0] = i.ToString();

Dr[1] = reader.GetValue(1);

Dr[2] = reader.GetValue(2);

Dr[3] = reader.GetValue(3);

Dr[4] = reader.GetValue(4);

Dr[5] = reader.GetValue(5);

Dr[6] = reader.GetValue(7);

Dr[7] = ConfigurationManager.AppSettings["OrgImgAnchor"];

dt.Rows.Add(Dr);

i += 1;

}

grdStatus.DataSource = dt;

grdStatus.DataBind();

connection.Close();


...S.VinothkumaR.

1 comment:

Bhaskar said...

thanks for need information.But you didn't mention how to do some operations data table.