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.
Step 1: Creating a DataTable.
- DataTable dt = new DataTable();
Step 2: Creating colums name or heading by mentioning the datatype.
- DataColumn dc1 = new DataColumn("PERSONAL NO", typeof(int));
- DataColumn dc2 = new DataColumn("NAME", typeof(string));
- DataColumn dc3 = new DataColumn("DATE", typeof(string));
- DataColumn dc4 = new DataColumn("QUANTITY", typeof(int));
- DataColumn dc5 = new DataColumn("TYPE", typeof(string));
Step 3: Adding these Columns to the DataTable,
- dt.Columns.Add(dc1);
- dt.Columns.Add(dc2);
- dt.Columns.Add(dc3);
- dt.Columns.Add(dc4);
- dt.Columns.Add(dc5);
Step 4: Creating a row that contail all value from the input elements.
- 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,
- 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.
- public void createnewrow()
- {
- DataTable dt = new DataTable();
- DataColumn dc1 = new DataColumn("PERSONAL NO", typeof(int));
- DataColumn dc2 = new DataColumn("NAME", typeof(string));
- DataColumn dc3 = new DataColumn("DATE", typeof(string));
- DataColumn dc4 = new DataColumn("QUANTITY", typeof(int));
- DataColumn dc5 = new DataColumn("TYPE", typeof(string));
- dt.Columns.Add(dc1);
- dt.Columns.Add(dc2);
- dt.Columns.Add(dc3);
- dt.Columns.Add(dc4);
- dt.Columns.Add(dc5);
- dt.Rows.Add(txt_personalNo.Text, txt_name.Text, txt_date.Text, Convert.ToInt32(txt_quantity.Text), cmb_type.SelectedItem.ToString());
- dataGridView1.DataSource = dt;
- }
Now call this Method On Book Button and click after filling all input.
- private void btn_book_Click(object sender, EventArgs e)
- {
- createnewrow();
- }
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.
- public partial class LunchDinnerBookingEntry : Form
- {
- CanteenBAL obj = new CanteenBAL();
- DataTable dt = new DataTable();
- public LunchDinnerBookingEntry()
- {
- InitializeComponent();
- }
Now the second change as per condition.
- public void createnewrow()
- {
- if(dt.Rows.Count<=0)
- {
-
- DataColumn dc1 = new DataColumn("PERSONAL NO", typeof(int));
- DataColumn dc2 = new DataColumn("NAME", typeof(string));
- DataColumn dc3 = new DataColumn("DATE", typeof(string));
- DataColumn dc4 = new DataColumn("QUANTITY", typeof(int));
- DataColumn dc5 = new DataColumn("TYPE", typeof(string));
-
-
- dt.Columns.Add(dc1);
- dt.Columns.Add(dc2);
- dt.Columns.Add(dc3);
- dt.Columns.Add(dc4);
- dt.Columns.Add(dc5);
-
-
-
- dt.Rows.Add(txt_personalNo.Text, txt_name.Text, txt_date.Text, Convert.ToInt32(txt_quantity.Text), cmb_type.SelectedItem.ToString());
-
-
- dataGridView1.DataSource = dt;
-
- }
- else
- {
-
- dt.Rows.Add(txt_personalNo.Text, txt_name.Text, txt_date.Text, Convert.ToInt32(txt_quantity.Text), cmb_type.SelectedItem.ToString());
-
-
- dataGridView1.DataSource = dt;
-
- }
-
-
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.