Dynamically Creating DataTable and Binding To GridView Without Database

For dynamically creating DataTable and binding to GridView without database, we have to know the following things:

  1. How to create columns in a DataTable.
  2. How to add rows in a DataTable.
Syntax For Creating Column in DataTable
    1. DataTable mytable=new DataTable();  
    2. mytable.columns.add("ColunmName",TypeOf(string/int/Float));   

Syntax For Creating Rows 

    1. DataTable mytable=new DataTable(); //creating datatable  
    2. DataRow dr=mytable.NewRow(); //Creating Row  
    3. dr["ColunmName"]="Raj"//assigning value to the row  
    4. mytable.Rows.add(dr); //adding to datatable   

Example

Step 1: Design the form as in the following image:

Design

Step 2: When the form is filled and enter button is hit, we want that the value should not be added in the database, but instead of this it should be directly added to my DataTable so that I could bind the DataTable and show directly in the
form.

Step 3: For this add a GridView in the form and name it as GridView1.

Step 4: Now under show button click, write the following code:

  1. protected void btn_1_Click(object sender, EventArgs e)  
  2. {  
  3.    GridView1.Visible = true//my grid view name  
  4.    createnewrow(); //call a function  
  5. }  
Step 5: Create the function createnewrow().
  1. public void createnewrow()   
  2. {  
  3.     DataTable mytable = new DataTable();  
  4.     if (ViewState["Row"] != null)   
  5.     {  
  6.         mytable = (DataTable) ViewState["Row"];  
  7.         DataRow dr = null;  
  8.         if (mytable.Rows.Count > 0)  
  9.         {  
  10.             dr = mytable.NewRow();  
  11.             dr["Id"] = txt_id.Text;  
  12.             dr["Name"] = txt_name.Text;  
  13.             dr["Location"] = txt_location.Text;  
  14.             dr["Salary"] = txt_salary.Text;  
  15.             mytable.Rows.Add(dr);  
  16.             ViewState["Row"] = mytable;  
  17.             GridView1.DataSource = ViewState["Row"];  
  18.             GridView1.DataBind();  
  19.         }  
  20.     }   
  21.     else   
  22.     {  
  23.         mytable.Columns.Add("Id"typeof(int));  
  24.         mytable.Columns.Add(new DataColumn("Name"typeof(string)));  
  25.         mytable.Columns.Add("Location"typeof(string));  
  26.         mytable.Columns.Add("Salary"typeof(float));  
  27.         DataRow dr1 = mytable.NewRow();  
  28.         dr1 = mytable.NewRow();  
  29.         dr1["Id"] = txt_id.Text;  
  30.         dr1["Name"] = txt_name.Text;  
  31.         dr1["Location"] = txt_location.Text;  
  32.         dr1["Salary"] = txt_salary.Text;  
  33.         mytable.Rows.Add(dr1);  
  34.         ViewState["Row"] = mytable;  
  35.         GridView1.DataSource = ViewState["Row"];  
  36.         GridView1.DataBind();  
  37.     }  
  38. }  
Now after show button click, it will directly show the record.

directly show the record

Explanation:

 

  1. For the first time the condition for the Viewstate is false, so the control goes to else part and creates the columns for the datatable.

  2. Create rows and add the Textbox value to them.

  3. Saving the table in viewstate.

  4. Binding the viewstate to the Gridview.

  5. After first time, the vewstate condition becomes true and it directly creates only rows and is added to the table.