Wednesday, 29 May 2013

Create run time DataTable and add rows,column and data to Datatable using c# code

Create run time DataTable and add rows,column and data to Datatable using c# code



Step 1 : First create your website in asp.net and create one .aspx page .

Step2  : Open .aspx page source code .cs file




Step 3: First add one import class file to access DataTable class

using System.Data;
 
Step 3 : Create one object of DataTable class

DataTable dt = new DataTable("Names");


Step 4 : Add rows and columns and data to data table

 DataTable dt = new DataTable("Names");
        DataColumn dc1 = new DataColumn("Name");
        DataColumn dc2 = new DataColumn("Age");
        dt.Columns.AddRange(new DataColumn[] { dc1, dc2 });
        DataRow dr1 = dt.NewRow();
        dr1[0] = "Ahmed";
        dr1[1] = "27";
        DataRow dr2 = dt.NewRow();
        dr2[0] = "Peter";
        dr2[1] = "30";
        DataRow dr3 = dt.NewRow();
        dr3[0] = "John";
        dr3[1] = "20";
        DataRow dr4 = dt.NewRow();
        dr4[0] = "Ali";
        dr4[1] = "30";

        DataRow dr5 = dt.NewRow();
        dr5[0] = "Ali";
        dr5[1] = "30";


        DataRow dr6 = dt.NewRow();
        dr6[0] = "Ali";
        dr6[1] = "30";


        DataRow dr7 = dt.NewRow();
        dr7[0] = "Ali";
        dr7[1] = "30";

        dt.Rows.Add(dr1);
        dt.Rows.Add(dr2);
        dt.Rows.Add(dr3);
        dt.Rows.Add(dr4);
        dt.Rows.Add(dr5);
        dt.Rows.Add(dr6);
        dt.Rows.Add(dr7);



Step 5: Access this table any where else in your code .






No comments:

Post a Comment

Thank you for your interest .