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 Specific Columns of Datatable to Datagridview in C# Windows Application in VB.NET

Aug 31, 2015
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.


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

Bind Specific columns of datatable to datagridview in c# windows application



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 :

Anonymous said...

good one

Give your Valuable Comments

Other Related Posts

© 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.