Here in this article we are going to discuss how to bind DataGrid in Windows form without Database.
In this article we are going to look into binding a datagrid directly from textbox and combobox without database connection.
This is an important topic regularly we need when working with windows forms application. Lets start to see how to do that.
Here's the design windows of our requirement.
Our requirement here is very simple ,when we enter all the fields and click on Book button it will temporary bind the data to the datagrid shown below.
For this, we have to follow the following steps-
- Creating a datatable.
- Creating colums name or heading by mentioning the datatype.
- Adding 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.
using System.Data;Lets create a method CreateRows() and call it on Book Button click.
public void createnewrow()
//Creating a object of datatable.
DataTable dt = new DataTable();
//Adding the Columns
new DataColumn("PERSONAL NO", typeof(string)),
new DataColumn("NAME", typeof(string)),
new DataColumn("DATE", typeof(string)),
new DataColumn("QUANTITY", typeof(string)),
new DataColumn("TYPE", typeof(String))
//Creating the row and adding the value in row
//finally binding to the datagrid.
dataGridView1.DataSource = dt;
Now on the Book button click, the following method is called as follows.
private void btn_book_Click(object sender, EventArgs e)
Now save the project and run the project.After entering and clicking it will give the following result.
In this way we can bind data directly from the input controls without any database connection.