• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 305
  • Last Modified:

How to bind data in gridview in vb.net.

I have a datasource, how to assign it to DataGridview in vb.net.
The datasource is ds.
0
searchsanjaysharma
Asked:
searchsanjaysharma
3 Solutions
 
DhaestCommented:
Is your datasource a dataset ? I assume that you want the first table

GridView1.DataSource = dsGrid.Tables(0)
GridView1.DataBind()
0
 
Peter KipropCommented:
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
 
Pratima PharandeCommented:
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
 
searchsanjaysharmaAuthor Commented:
thx
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now