Introduction:
Here I will explain how to bind specific columns of datatable to datagridview in windows application using c#, vb.net with example or bind / show particular or specific columns in datagridview from datatable in windows application in c#, vb.net with example.
Description:
In previous articles I explained bind datagridview in windows application in c#, vb.net, export gridview with images in asp.net using c#, vb.net, 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 specific columns of datatable to datagridview in windows application using c#, vb.net with example.
In previous articles I explained bind datagridview in windows application in c#, vb.net, export gridview with images in asp.net using c#, vb.net, 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 specific columns of datatable to datagridview in windows application using c#, vb.net with example.
To
bind specific columns to 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.AutoGenerateColumns = false;
dataGridView1.ColumnCount = 2;
dataGridView1.Columns[0].HeaderText = "Product
Id";
dataGridView1.Columns[0].DataPropertyName = "productid";
dataGridView1.Columns[1].HeaderText = "Product
Name";
dataGridView1.Columns[1].DataPropertyName = "productname";
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.AutoGenerateColumns = false;
dataGridView1.ColumnCount = 2;
dataGridView1.Columns[0].HeaderText = "Product
Id";
dataGridView1.Columns[0].DataPropertyName = "productid";
dataGridView1.Columns[1].HeaderText = "Product
Name";
dataGridView1.Columns[1].DataPropertyName = "productname";
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 :
good one
Note: Only a member of this blog may post a comment.