Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 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
Introduction

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

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

To bind datagridview in windows application in c#, vb.net 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()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
BindDataGridview();
}
protected void BindDataGridview()
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection("Data Source=Suresh;Integrated Security=true;Initial Catalog=MySampleDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from productinfo", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
dataGridView1.DataSource = dt;
}
}

VB.NET Code


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

Demo

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 Asp.net, C#.net, 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

1 comments :

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 vb.net

Give your Valuable Comments

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

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