Introduction:
Here I will explain how to bind data to datagridview in windows application in c#, vb.net or bind datagridview in c# windows application or bind datagridview in windows form or application using c#, vb.net.
Description:
In previous articles I explained Delete multiple rows in gridview using checkbox in asp.net, Get selected row cell value from gridview in asp.net, retrieve images from path stored in database in asp.net, Get textbox value palced in gridview in asp.net and many articles relating to asp.net, c#,vb.net and jQuery. Now I will explain how to bind data to datagridview in windows application in c#, vb.net.
In previous articles I explained Delete multiple rows in gridview using checkbox in asp.net, Get selected row cell value from gridview in asp.net, retrieve images from path stored in database in asp.net, Get textbox value palced in gridview in asp.net and many articles relating to asp.net, c#,vb.net and jQuery. Now I will explain how to bind data to datagridview in windows application in c#, vb.net.
Before
implement this example first design one table UserDetails in your database as
shown below
Column Name
|
Data Type
|
Allow Nulls
|
UserId
|
Int(IDENTITY=TRUE)
|
Yes
|
UserName
|
varchar(50)
|
Yes
|
FirstName
|
varchar(50)
|
Yes
|
LastName
|
varchar(50)
|
Yes
|
Once
table created in database enter some dummy data to test application after that create
new windows form application project for that follow steps
Open
visual studio à Go to File Menu à New à select Project à select windows from project type in Visual C# à Select Windows Forms Application template à Give name to your application
à Click OK like as shown in below image
Now drag and drop datagridview control from Toolbox
like as shown below
Once we placed DataGridView in our Form that will be
like as shown below
Now right click on your page and select View Code
option. In code behind add following namespaces
C#
Code
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
|
VB.NET
Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms
|
Once you add namespaces write following code in your
page
C#
Code
public partial class BindDataGridview
: Form
{
public BindDataGridview()
{
InitializeComponent();
BindData();
}
// Bind Data to DataGridView
private void BindData()
{
DataTable dt = new DataTable();
using (SqlConnection
con = new SqlConnection("Data Source=SureshDasari;Integrated
Security=true;Initial Catalog=MySampleDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select
* from UserDetails", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
}
dataGridView1.DataSource = dt;
}
}
|
VB.NET
Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms
Public Class Form1
Public Sub New()
InitializeComponent()
BindData()
End Sub
' Bind Data to DataGridView
Private Sub
BindData()
Dim dt As New DataTable()
Using con As New SqlConnection("Data
Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
con.Open()
Dim cmd As New SqlCommand("Select
* from UserDetails", con)
Dim da As New SqlDataAdapter(cmd)
da.Fill(dt)
con.Close()
End Using
dataGridView1.DataSource = dt
End Sub
End Class
|
Once you finished coding run your application and check
output that will be like as shown below
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. |
|||
|
|||
0 comments :
Note: Only a member of this blog may post a comment.