Binding DataGrid In Windows Form Without Database

Debendra256
Posted by Co Author(s): Debendra256 in C# category on for Intermediate level | Points: 250 | Views : 624 red flag
Rating: 4 out of 5  
 2 vote(s)

Here in this article we are going to discuss how to bind DataGrid in Windows form without Database.

Introduction

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

            dt.Columns.AddRange(new DataColumn[5]
               {
                   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
            dt.Rows.Add(txt_personalNo.Text,txt_name.Text,txt_date.Text,txt_quantity.Text,cmb_type.SelectedItem.ToString());
           //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)
        {
            createnewrow();
        }

Now save the project and run the project.After entering and clicking it will give the following result.


Conclusion

In this way we can bind data directly from the input controls without any database connection.



Page copy protected against web site content infringement by Copyscape

About the Author

Debendra256
Full Name: Debendra Dash
Member Level: Starter
Member Status: Member
Member Since: 10/9/2015 7:13:47 AM
Country: India
Debendra Dash

Nearly 3 years of experiance in various Microsoft Technology.Currently working as a Technical Consultant in R2 international India Pvt Ltd.


About the Co-Author(s)

By debendra256 debendra256 Points: 1700 | Level: Starter | Status: [Member]

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)