DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

Containers Trend Report: Explore the current state of containers, containerization strategies, and modernizing architecture.

Low-Code Development: Learn the concepts of low code, features + use cases for professional devs, and the low-code implementation process.

E-Commerce Development Essentials: Considering starting or working on an e-commerce business? Learn how to create a backend that scales.

Related

  • Handle Sensitive Data Securely With Skyflow
  • APEX_COLLECTION: APEX's Super Power!
  • Super Dynamic APEX Forms Using APEX_ITEM and Dynamic Actions
  • Spring Security 5 Form Login With Database Provider

Trending

  • CI/CD Software Design Patterns and Anti-Patterns
  • Scaling SRE Teams
  • Modern Application Performance
  • Send Time Optimization
  1. DZone
  2. Data Engineering
  3. Databases
  4. Binding DataGrid In Windows Form Without Database

Binding DataGrid In Windows Form Without Database

Look at how to bind datagrid in windows form without database.

Debendra Dash user avatar by
Debendra Dash
·
Jun. 25, 18 · Tutorial
Like (2)
Save
Tweet
Share
13.0K Views

Join the DZone community and get the full member experience.

Join For Free

In 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:

Image title

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();  
} 

Image title

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;  

            }  

Image title

Thus, in this way, we can also maintain state in windows application.

Database Form (document) Binding (linguistics)

Opinions expressed by DZone contributors are their own.

Related

  • Handle Sensitive Data Securely With Skyflow
  • APEX_COLLECTION: APEX's Super Power!
  • Super Dynamic APEX Forms Using APEX_ITEM and Dynamic Actions
  • Spring Security 5 Form Login With Database Provider

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: