Binding DataGrid In Windows Form Without Database
Look at how to bind datagrid in windows form without database.
Join the DZone community and get the full member experience.
Join For FreeIn one of my articles, I explained how to bind a gridview in web form without a database. Here, I am now going to explain how to bind a datagrid in windows form without a database.
This requirement is very useful when we are working in windows form or web form. My requirement here is very simple. When we enter all the fields and click on the Book button. It will temporarily bind the data to the data grid shown below. I have shown the screenshot below:
Let's see how to do that; for doing this, you have to follow the following steps.
- Create a datatable.
- Create a column name or heading by mentioning the datatype.
- Add this column to the datatable
- Create a row that contains all the values from the input controls.
- Bind the datatable to the Datagrid.
Before using this, we need a namespace in our form.
using System.Data;
Step 1: Create a DataTable.
DataTable dt = new DataTable();
Step 2: Create column 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 contains all values 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. I have 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();
}
So in this way, we can bind a datagrid without a database in windows form application.
For maintaining the state of this datagrid, Now what is the problem the user are facing.
After entering the details of first my first booking, when I am doing my second booking, the first booking 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 data table globally.
- Put a condition while you are binding rows to the datagrid. First, check to see if there is data in that datatable. If there is no data, bind the columns header in the 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;
}
Thus, in this way, we can also maintain state in windows application.
Opinions expressed by DZone contributors are their own.
Comments