Binding DataGrid In Windows Form Without Database

Binding DataGrid In Windows Form Without Database

In one of my articles I  have explained how to bind a gridview in web form without database. Here I am now going to explain how to bind datagrid in a windows form without database.

This requirement is very useful when we are working in windows form or webform. Here my requirement here is very simple, when we enter all the fields and click on Book button it will temporarily bind the data to the datagrid shown below. Here I have shown the screen shot as follows.

Let us see how to do that; for doing this this you have to follow the following steps. 
  • Creating a datatable.
  • Creating colums name or heading by mentioning the datatype.
  • Adding these column to the datatable
  • Creating a row that contain all the value from the input controls.
  • Binding the datatable to the Datagrid. 
Let's start with the code:

Before using this we need a namespace in our form.
  1. using System.Data;    
Step 1: Creating a DataTable.
  1. DataTable dt = new DataTable();  
Step 2: Creating colums name or heading by mentioning the datatype. 
  1. DataColumn dc1 = new DataColumn("PERSONAL NO"typeof(int));    
  2. DataColumn dc2 = new DataColumn("NAME"typeof(string));    
  3. DataColumn dc3 = new DataColumn("DATE"typeof(string));    
  4. DataColumn dc4 = new DataColumn("QUANTITY"typeof(int));    
  5. DataColumn dc5 = new DataColumn("TYPE"typeof(string));   
Step 3: Adding these Columns to the DataTable,
  1. dt.Columns.Add(dc1);    
  2. dt.Columns.Add(dc2);    
  3. dt.Columns.Add(dc3);    
  4. dt.Columns.Add(dc4);    
  5. dt.Columns.Add(dc5);   
Step 4: Creating a row that contail all value from the input elements.
  1. dt.Rows.Add(txt_personalNo.Text,txt_name.Text,txt_date.Text,Convert.ToInt32(txt_quantity.Text),cmb_type.SelectedItem.ToString());  
Step 5: Binding the datatable to datagrid,
  1. dataGridView1.DataSource = dt;  
So here is my complete code. Ihave to create a method and do all these inside this and call the method on the button Click. Here is my method.
  1. public void createnewrow()  
  2. {  
  3.     DataTable dt = new DataTable();  
  4.     DataColumn dc1 = new DataColumn("PERSONAL NO"typeof(int));  
  5.     DataColumn dc2 = new DataColumn("NAME"typeof(string));  
  6.     DataColumn dc3 = new DataColumn("DATE"typeof(string));  
  7.     DataColumn dc4 = new DataColumn("QUANTITY"typeof(int));  
  8.     DataColumn dc5 = new DataColumn("TYPE"typeof(string));  
  9.     dt.Columns.Add(dc1);  
  10.     dt.Columns.Add(dc2);  
  11.     dt.Columns.Add(dc3);  
  12.     dt.Columns.Add(dc4);  
  13.     dt.Columns.Add(dc5);  
  14.     dt.Rows.Add(txt_personalNo.Text, txt_name.Text, txt_date.Text, Convert.ToInt32(txt_quantity.Text), cmb_type.SelectedItem.ToString());  
  15.     dataGridView1.DataSource = dt;  
  16. }  
Now call this Method On Book Button and click after filling all input.
  1. private void btn_book_Click(object sender, EventArgs e)  
  2. {  
  3. createnewrow();  
  4. }  
It will be like this.

So in this way we can bind a Datagrid without database in windows form Application.
Here i am updating my blog as per i got a Question  about maintaining state of this datagrid.Now what is the problem the user are facing.
After Enterning the details of first  my first Booking,when  i am doing my Second Booking tha first boooking details are lost,so to prevent this you have to change your  code a little bit. Here i have explained how to do that.
  • Declare the Datatable globally.
  •  Put a condition while you are binding rows to the datagrid.First check if there is data in tha datatable or not if there is no data bind the columns header in datagrid else bind only the row without datacolumn header.
    So here is the First change,Declare the datatable globally.
    1. public partial class LunchDinnerBookingEntry : Form  
    2.   {  
    3.       CanteenBAL obj = new CanteenBAL();  
    4.        DataTable dt = new DataTable();  
    5.       public LunchDinnerBookingEntry()  
    6.       {  
    7.           InitializeComponent();  
    8.       }  
    Now the second change as per condition.

    1. public void createnewrow()  
    2.         {  
    3.             if(dt.Rows.Count<=0)  
    4.             {  
    6.                 DataColumn dc1 = new DataColumn("PERSONAL NO"typeof(int));  
    7.                 DataColumn dc2 = new DataColumn("NAME"typeof(string));  
    8.                 DataColumn dc3 = new DataColumn("DATE"typeof(string));  
    9.                 DataColumn dc4 = new DataColumn("QUANTITY"typeof(int));  
    10.                 DataColumn dc5 = new DataColumn("TYPE"typeof(string));  
    13.                 dt.Columns.Add(dc1);  
    14.                 dt.Columns.Add(dc2);  
    15.                 dt.Columns.Add(dc3);  
    16.                 dt.Columns.Add(dc4);  
    17.                 dt.Columns.Add(dc5);  
    21.                 dt.Rows.Add(txt_personalNo.Text, txt_name.Text, txt_date.Text, Convert.ToInt32(txt_quantity.Text), cmb_type.SelectedItem.ToString());  
    24.                 dataGridView1.DataSource = dt;  
    26.             }  
    27.             else  
    28.             {  
    30.                 dt.Rows.Add(txt_personalNo.Text, txt_name.Text, txt_date.Text, Convert.ToInt32(txt_quantity.Text), cmb_type.SelectedItem.ToString());  
    33.                 dataGridView1.DataSource = dt;  
    35.             }  
    Now here i have attached the Screen shot demonstrating how it start works by maintaining state.

    Thus in this way we can also maintaining state in windows application.