Runtime Bind Data from SQL Server Table to Datagridview in Windows Application in C# VB.NET

DataGridView is an essential control for windows applications. This control can be used in both C#.Net windows applications as well as VB.Net Applications. Specially we use this control for tabular data representation. Here in this article I will show “How to Bind SQL Server Table Data to Datagridview in Windows Applications“. To do the example we need to create a Table first. After that from run time we will bind the SQL Server Table Data into the DataGridView control.

SQL Server Table:
Bind Data into Datagridview Csharp 1

Add Connectionstring into App.config File:
For C#.Net:
Right click on Application then go to Add >> Add New Item. Now select “Application configuration File” and click on “Add” button like below:
Bind Data into Datagridview Csharp 3

Now in solution explorer you will get “App.config” file. Modify the file like:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="DBConnection" value="Data Source=.\SQLEXPRESS;Initial Catalog=TESTDB;Trusted_Connection=yes;"/>
    <add key="DBConnection2" value="Data Source=XXX.com;Initial Catalog=DBNAME;User Id=UserName;Password=YourPassword;"/>
  </appSettings>
</configuration>

Note: If you use windows authentication then use the first key. Otherwise use second key line. Update server name, Database name, User name, Password as per your settings.

For VB.Net:
Go to the application folder. “App.config” file automatically created into the folder. Add this file in your project as a reference. To do that right click on Application from solution explorer. Then go to Add >> Existing Item & browse to choose the “App.config” file.

Now in solution explorer you will get “App.config” file. Modify the file like:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client" />
    </startup>

  <appSettings>
    <add key="DBConnection" value="Data Source=.\SQLEXPRESS;Initial Catalog=TESTDB;Trusted_Connection=yes;"/>
    <add key="DBConnection2" value="Data Source=XXX.com;Initial Catalog=DBNAME;User Id=UserName;Password=YourPassword;"/>
  </appSettings>
</configuration>

Note: If you use windows authentication then use the first key. Otherwise use second key line. Update server name, Database name, User name, Password as per your settings.

Adding Namespaces:
1. System.Configuration
2. System.Data.SqlClient

Now add a DataGridView control in your form. Under the Form_Load method bind the control in the following way:
C# Code:

        private void Form1_Load(object sender, EventArgs e)
        {
            string consString = ConfigurationSettings.AppSettings["DBConnection"];
            using (SqlConnection conn = new SqlConnection(consString))
            {
                SqlCommand cmd = new SqlCommand("SELECT * FROM [Students]", conn);
                cmd.CommandType = CommandType.Text;
                conn.Open();
                SqlDataReader drStudents = cmd.ExecuteReader();
                DataTable dt = new DataTable();
                dt.Load(drStudents);
                dataGridView1.DataSource = dt;
                dataGridView1.Refresh();
            }
        }

VB.Net Code:

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim consString As String = ConfigurationSettings.AppSettings("DBConnection")
        Using conn As New SqlConnection(consString)
            Dim cmd As New SqlCommand("SELECT * FROM [Students]", conn)
            cmd.CommandType = CommandType.Text
            conn.Open()
            Dim drStudents As SqlDataReader = cmd.ExecuteReader()
            Dim dt As New DataTable()
            dt.Load(drStudents)
            DataGridView1.DataSource = dt
            DataGridView1.Refresh()
        End Using
    End Sub

Output:
Bind Data into Datagridview Csharp

Hope now you can “Bind Data from SQL Server Table to Datagridview in Windows Application using C# VB.NET“.

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, C#, Database, DataGridView, SQL Server, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code
     
 

*