Solved

How to bind data in gridview in vb.net.

Posted on 2012-03-12
4
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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