Results 1 to 3 of 3

Thread: Displaying result in a datagrid

  1. #1
    Meera is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    Jun 2010
    Posts
    11
    Reputation
    38

    Displaying result in a datagrid

    Hi all,
    i am trying to bring a query result as a datagrid. I am new to vb.net.
    SQL 2005, Visual studio 2005
    I have a sql query to display 3 cols of value. the below is my code
    ------------------------------------------------------------------------
    Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim ConString As String = "Data Source=<>;Initial Catalog='<>';User Id=sa;Password=<>;"
    Dim MyCon As New SqlConnection
    MyCon.ConnectionString = ConString
    Dim DA As New SqlDataAdapter("Select [No_], [Name], [City] from [dbo].[Customer]", MyCon)
    Dim DS As New DataSet
    DA.Fill(DS, "[dbo].[Customer]")
    Dim DataGridView1 As New DataGridView
    DataGridView1.DataSource = DS.DefaultViewManager
    End Sub

    -------------------------------------------------------
    Kindly bear with my question.
    What i did is at first i dint have a datagrid, drag n dropped in the form.
    When executing the code the results not getting populated to the datagrid.
    please guide me where im going wrong with the codings?

    Many Thanks,
    Meera.

  2. #2
    sdavara is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    Jun 2010
    Posts
    1
    Reputation
    0
    hi, first you have couple of choice to display your data in the datagrid,
    1)Drag data grid on the form and set colums which you want to show,
    2)Dynamically create grid and add to your forms and add columns to it.

    Once you have grid, you just need to set its data source and it will show your data to the datagrid.
    in above code better you add datagrid to your form by drag and drop then just set datasource, let me remind you one more thing, if you have not added any columns then it will show all of the columns of your datasource.

  3. #3
    Meera is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    Jun 2010
    Posts
    11
    Reputation
    38
    Hi, Thanks for your reply.
    Could you please explain me how to set columns in the grid?
    Also my grid doesn't have any columns , but when debugged no rows or columns are displayed. Kindly let me know how to set the datasource to the grid apart from being with query.

    Thanks !!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking