Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Problem with DataGridView in vb.net

Options
  • 28-04-2016 1:07pm
    #1
    Registered Users Posts: 8,040 ✭✭✭


    Right I've a weird issue. DataGridView is displaying a join from two database tables.

    When performing this query through SQL on the server (through ssms) the displayed output columns are:
    Name, Staff,1,2,3,..,30
    
    However in the DataGridView in the application it's displaying:
    Staff,1,2,3,..,30,Name 
    
    Slightly different order where the Name column is now at the end.

    After some experimentation I have come to realise that the column "Name" is attached to column "30" (thus sharing the same displayIndex).
    Thus setting:
    Datagridview.column("30").DisplayIndex = 0
    
    Will display:
    30,name,Staff,1,2,..29
    

    So my question (finally)is why is this happening. Is it due to my SQL Join Query? (please excuse the naming mess)
    SELECT Staff.id, Avril2016.*
    FROM Staff
    JOIN Avril2016 ON
    Staff.id = Avril2016.staff
    

    Been trying to figure this out for hours and I'm about to throw in the towel.
    Any one got any ideas please?


Comments

  • Registered Users Posts: 11,977 ✭✭✭✭Giblet


    Are binding columns or adding any dynamically?


  • Registered Users Posts: 8,040 ✭✭✭BKtje


    Thanks for having a look.

    Data is pulled from the database using an SQLDataAdapter and then added to the grid automatically. Nothing else is done to it before the problem appears. I hope that answers the question.

    Relevant (I believe) code snippets are as follows:
                    mainForm.mySqlControl.SQLGridQuery("SELECT Staff.Name, " & myMonth & ".* FROM Staff JOIN " & myMonth & " ON Staff.id = " & myMonth & ".Staff ORDER BY Staff.id")
    
                    If (mainForm.mySqlControl.myDataSet.Tables.Count > 0) Then
                        planGrid.DataSource = mainForm.mySqlControl.myDataSet.Tables(0)
                    End If
    
                myCon.Open()
    
                'Create the sql query to be sent and which connection to use
                mySqlCmd = New SqlCommand(Query, myCon)
    
                'Add the parameters to the sql command
                myParams.ForEach(Sub(x) mySqlCmd.Parameters.Add(x))
    
    
                'Clear Parameter list
                myParams.Clear()
    
                'Execute and fill the DataSet using the DataAdapter
                myDA = New SqlDataAdapter(mySqlCmd)
                myDataSet = New DataSet
    
                'records the number of rows in the DataSet
                recordCount = myDA.Fill(myDataSet)
    

    I imagine it's something stupid as I'm really quite new to connecting to databases and populating grids from it (outside of Uni confines). I can survive as is but harder to understand the grid.


  • Registered Users Posts: 203 ✭✭Sherfin


    I don't think it's anything you're doing. Because you haven't specified any order it will do what it wants. From MSDN
    "When you use a DataGridView to display data from a data source, the columns in the data source's schema sometimes do not appear in the order you would like to display them. You can change the displayed order of the columns by using the DisplayIndex property of the DataGridViewColumn class"

    Same with the query itself, if you didn't have the order by clause, you may get your results in a different order anytime you run the query


Advertisement