How can generate dynamic data table in c#

Posted by Sekar.C under C# on 7/26/2012 | Points: 10 | Views : 12932 | Status : [Member] | Replies : 3
Hi,
How can generate dynamic data table in c#.

Regards
Sekar.c



Responses

Posted by: Ranjeet_8 on: 7/26/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
       

DataTable dt = new DataTable();
dt.Columns.Add("EmpCode", typeof(int));
dt.Columns.Add("EmpName", typeof(string));
dt.Columns.Add("InsertedDate", typeof(DateTime));

// Here we add 5 DataRows.

dt.Rows.Add(1001, "AAA", DateTime.Now);
dt.Rows.Add(1002, "BBB", DateTime.Now);
dt.Rows.Add(1003, "CCC", DateTime.Now);
dt.Rows.Add(1004, "DDD", DateTime.Now);
dt.Rows.Add(1005, "EEE", DateTime.Now);


Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prtkaggrwl on: 7/26/2012 [Member] Starter | Points: 25

Up
0
Down
DataTable dt = new DataTable();
dt.Columns.Add("ECode");
dt.Columns.Add("EName");

DataRow dr = dt.NewRow();
dr ("ECode") = "0001";
dr("EName") = "Prateek Aggarwal"
dt.Rows.Add(dr);

Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Megan00 on: 7/26/2012 [Member] Starter | Points: 25

Up
0
Down
Hey, below code:
          public void DynamicDataTable()

{
DataTable dataTable = new System.Data.DataTable();
DataColumn column1 = new System.Data.DataColumn("Id", typeof(System.Int32));
dataTable.Columns.Add(column1);
DataColumn column2 = new System.Data.DataColumn("Name", typeof(System.String));
dataTable.Columns.Add(column2);
DataColumn column3 = new System.Data.DataColumn("Age", typeof(System.Byte));
dataTable.Columns.Add(column3);
DataRow row1 = dataTable.NewRow();
row1["Id"] = 0;
row1["Name"] = "Tom";
row1["Age"] = 12;
dataTable.Rows.Add(row1);
DataRow row2 = dataTable.NewRow();
row2["Id"] = "1";
row2["Name"] = "Mike";
row2["Age"] = 13;
dataTable.Rows.Add(row2);
this.dataGridView1.DataSource = dataTable;

}


Never give up! Smile to the world!
http://excelcsharp.blog.com/

Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response