aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Bind (Show) Datatable to DataGridview in Windows Application in C#, VB.NET

Aug 30, 2015

Here I will explain how to bind datagridview in windows application using
c#, with example or bind datatable to datagridview in windows application in c#, with example.

In previous articles I explained
insert, update, delete in gridview with single stored procedure, jQuery gridview crud operations without postback in, gridview examples in, display images from database using handler in, generate random password in using c# and many articles relating to gridview,, c#, and jQuery. Now I will explain how to bind datatable to datagridview in windows application using c#, with example.

To bind datagridview in windows application in c#, first create new windows form application and then drag and drop datagridview from toolbox like as shown below

Datagridview in toolbox in windows application
Now add following namespaces in code behind

C# Code

using System;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;

After completion of adding namespaces you need to write the code like as shown below

public Form1()
private void Form1_Load(object sender, EventArgs e)
protected void BindDataGridview()
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection("Data Source=Suresh;Integrated Security=true;Initial Catalog=MySampleDB"))
SqlCommand cmd = new SqlCommand("select * from productinfo", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
dataGridView1.DataSource = dt;


Imports System.Windows.Forms
Imports System.Data.SqlClient
Imports System.Data
Public Class Form1
Public Sub New()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
End Sub
Protected Sub BindDataGridview()
Dim dt As New DataTable()
Using con As New SqlConnection("Data Source=Suresh;Integrated Security=true;Initial Catalog=MySampleDB")
Dim cmd As New SqlCommand("select * from productinfo", con)
Dim da As New SqlDataAdapter(cmd)
dataGridView1.DataSource = dt
End Using
End Sub
End Class


Bind (Show) Datatable to DataGridview in Windows Application in C#, VB.NET

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Unknown said...

Hi Suresh, i have created gridview with hyperlink and want to pass values from grid view to another page with text boxes and dropdown list. so i know that should add datatable can you tell me how to add datatable in the above code. i am doing in

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.