Results 1 to 12 of 12

Thread: Data Grid coding not working properly!!!

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Unhappy Data Grid coding not working properly!!!

    I did this code:
    Code:
    Imports System.Data
    Imports System.Data.OleDb
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Gautam\Documents\Visual Studio 2005\Projects\waste\waste\db1.mdb"
            Dim myConnection As OleDbConnection = New OleDbConnection
            myConnection.ConnectionString = connString
            Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select * from Table1", myConnection)
            Dim ds As DataSet = New DataSet
            da.Fill(ds, "Table1")
            DataGridView1.DataSource = ds.DefaultViewManager
        End Sub
    End Class
    I get this output on button click:
    Attachment 72031

    Where does the error lies in my code?
    Last edited by gautamshaw; Feb 21st, 2010 at 01:22 PM.

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Data Grid coding not working properly!!!

    This line of code
    Code:
    DataGridView1.DataSource = ds.DefaultViewManager
    Should be changed to this:
    Code:
    DataGridView1.DataSource = ds.Tables("Table1")
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    Re: Data Grid coding not working properly!!!

    The datasoure of datagridview belong to datatable format.
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Unhappy Re: Data Grid coding not working properly!!!

    Sorry Stanav......
    a new set of are add on doing this:
    Code:
    DataGridView1.DataSource = ds.Tables("Table1")
    the output is:
    Attachment 72032
    How to rectify this?
    Last edited by gautamshaw; Feb 21st, 2010 at 01:22 PM.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Re: Data Grid coding not working properly!!!

    Code:
    The datasoure of datagridview belong to datatable format
    Then where should I rectify my code Manhit?

  6. #6
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    Re: Data Grid coding not working properly!!!

    Quote Originally Posted by gautamshaw View Post
    Code:
    The datasoure of datagridview belong to datatable format
    Then where should I rectify my code Manhit?
    Code:
    Imports System.Data
    Imports System.Data.OleDb
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Gautam\Documents\Visual Studio 2005\Projects\waste\waste\db1.mdb"
            Dim myConnection As OleDbConnection = New OleDbConnection
            myConnection.ConnectionString = connString
            Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select * from Table1", myConnection)
            Dim ds As DataSet = New DataSet
            da.Fill(ds, "Table1")
    dim dt as new datatable
    da.fill(dt)
            DataGridView1.DataSource = dt
        End Sub
    End Class
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Re: Data Grid coding not working properly!!!

    Sorry manhit.....
    again the same problem...
    a new set of columns are added and the output looks like the above image that i posted!!!

    Code still not working.....

  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Data Grid coding not working properly!!!

    OK. You need to set the datapropertyname property to your datagridview columns.
    Add these lines:
    Code:
    DataGridView1.Columns(0).DataPropertyName = "Name"
    DataGridView1.Columns(1).DataPropertyName = "Address"
    DataGridView1.Columns(2).DataPropertyName = "Roll"
    Right before this line
    Code:
    DataGridView1.DataSource = ds.Tables("Table1")
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Unhappy Re: Data Grid coding not working properly!!!

    You did not understand my question......Or may be i did not explained it to you properly.......I am sorry for that.....
    If my form at design time looks like this then:
    Attachment 72033
    Your code is perfect
    But my design time form is like this:
    Attachment 72034
    I just want to place the values from the database to the grid columns inspite of adding the entire database columns to the grid.

    How to do this?
    Last edited by gautamshaw; Feb 21st, 2010 at 01:22 PM.

  10. #10
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Data Grid coding not working properly!!!

    I knew this from looking at the screenshot in your last post. That's why I told you to set the columns's datapropertyname property.
    Did you try what I said in the last post? You can add those 3 lines before or after the line you set the datagridview.datasource property, but adding them before gives you a slight performance advantage. And that's why I told you to add the lines before you bind the datagidview.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Unhappy Re: Data Grid coding not working properly!!!

    yes i added the three lines that you gave and it worked excellently.......

    Thanks stanav

    Now i want to add a print button so that i can print the datagridview contents.......

    How to do this?

  12. #12
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    Re: Data Grid coding not working properly!!!

    Do you want use crystal report or excel to view.

    creat other new thread.
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

Posting Permissions

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



Click Here to Expand Forum to Full Width