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.
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
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
|
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. |
|||
|
|||
1 comments :
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
Note: Only a member of this blog may post a comment.