Solved

How to bind data in gridview in vb.net.

Posted on 2012-03-12
4
293 Views
Last Modified: 2012-06-21
I have a datasource, how to assign it to DataGridview in vb.net.
The datasource is ds.
0
Comment
Question by:searchsanjaysharma
4 Comments
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 167 total points
ID: 37713325
Is your datasource a dataset ? I assume that you want the first table

GridView1.DataSource = dsGrid.Tables(0)
GridView1.DataBind()
0
 
LVL 6

Assisted Solution

by:Peter Kiprop
Peter Kiprop earned 166 total points
ID: 37713338
Hi searchsanjaysharma,

You can bind it as below. i am assuming you are using dataset.

DataGridView1.DataSource=Ds.Tables("tablename")
DataGridView1.DataBind()

Open in new window

0
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 167 total points
ID: 37713405
IF it is windows form .. foloow this
Imports System.Data.SqlClient
Public Class Form1
    Private Const ConnectionString As String = "Server=.\SQLEXPRESS;" & _
                    "Database=NorthWind;Trusted_Connection=True"
    Private ReadOnly Property Connection() As SqlConnection
        Get
            Dim ConnectionToFetch As New SqlConnection(ConnectionString)
            ConnectionToFetch.Open()
            Return ConnectionToFetch
        End Get
    End Property
    Public Function GetData() As DataView
        Dim SelectQry = "SELECT * FROM Products "
        Dim SampleSource As New DataSet
        Dim TableView As DataView
        Try
            Dim SampleCommand As New SqlCommand()
            Dim SampleDataAdapter = New SqlDataAdapter()
            SampleCommand.CommandText = SelectQry
            SampleCommand.Connection = Connection
            SampleDataAdapter.SelectCommand = SampleCommand
            SampleDataAdapter.Fill(SampleSource)
            TableView = SampleSource.Tables(0).DefaultView
        Catch ex As Exception
            Throw ex
        End Try
        Return TableView
    End Function
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DataGridView1.DataSource = GetData()
    End Sub
End Class


Refer links
http://www.vbnettutorial.net/?Id=125&Desc=VB.Net-DataGridView
http://msdn.microsoft.com/en-us/library/fbk67b6z.aspx

Open in new window

0
 

Author Closing Comment

by:searchsanjaysharma
ID: 37726171
thx
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now