Datagridview: Sort on loading form?

Sean909

New member
Joined
Dec 30, 2008
Messages
4
Programming Experience
Beginner
Hi there,

I have this datagridview filled with data from a csv file. Clicking the headers of the DGV is sorting the columns. So far so good, but what I want is the DGV automatically gets sorted in column 1, when the form opens, without having to click the header.

VB.NET:
Form1.DataGridView1.Sort(Form1.DataGridView1.Columns(0), ListSortDirection.Descending)

gives the error:
'Data-bound datagridview control can only be sorted on data-bound columns'

ok..Assuming that 1st column is not bound then how to order on that column in vb-code?

Thanks !

Full code:
VB.NET:
Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & pname & "\;Extended Properties='text;HDR=no;FMT=Delimited'"
        Dim objConn As New OleDb.OleDbConnection(sConnectionString)
        objConn.Open()
        Dim objCmdSelect As New OleDb.OleDbCommand(stringsamen & fname, objConn)
        Dim objAdapter1 As New OleDb.OleDbDataAdapter
        objAdapter1.SelectCommand = objCmdSelect

        Dim objDataset1 As New DataSet()
        Try
           
            objAdapter1.Fill(objDataset1, "Table1")
            Form1.DataGridView1.DataSource = objDataset1.Tables(0)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        
        objConn.Close()

Form1.DataGridView1.Sort(Form1.DataGridView1.Columns(1), ListSortDirection.Descending)
 
How does it come to be that the column is not bound? I see nothing in your code that implies it is an unbound column
 
How does it come to be that the column is not bound? I see nothing in your code that implies it is an unbound column

you're right. The code I posted implies that the columns are indeed databound. That confused me too. What I missed was that another subroutine added 2 extra columns ...and I tried doing the sorting with those 2 columns, which of course are not databound. Sorting with the other columns does work..problem solved.:)
thanks for your reply !
 
Back
Top