?
Solved

How to bind data in gridview in vb.net.

Posted on 2012-03-12
4
Medium Priority
?
301 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 668 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 664 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 668 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

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.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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 …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

762 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