Hi!

I have an bound datagridview in vb.net. I have it pre-formatted the way I want it. Like for example the .displayIndex which columns will come first, etc. This works fine when the form is first loaded. But when it is loaded again, the format is being resetted.

Sample: - load frmDataGridView.show '1st Click format is correct
- load it again, '2nd click, format is reset

Kindly help me please, thanks!

What do you mean by displayindex?
DataSet would be typed or not. What is yours?
If your DataSet is nontyped, Re-arrange the field names at SQL Statement as your desired serial.

This is a sample code. This is how my code executes.

dg.DataSource = Nothing
dg.ColumnCount = 0

sqlGet = "SELECT * FROM table" 

With cmdGet
    .CommandText = sqlGet
    .Connection = conn
End With

daGet.SelectCommand = cmdGet
daGet.Fill(dsGet, tblname)

dg.DataSource = dsGet
dg.DataMember = tblname

dgformat()

displayIndex - I just said this as an example. Because, I have pre-formatted the order of columns to be displayed on my datagridview. On first load, the format is being executed, but on second load (load the form then close the form then load again), the format is being executed as I insert a breakpoint to it but the order of columns to be displayed is not.

Example: first load: Column1, Column2, Column3
second load: Column2, column1, column3

Write the column siquence as you desire at SQL Statement.
Like: "Select Col1, Col2, Col3,......... From TableName"
Or "Select Col2, Col1, Col3,......... From TableName" Or "Select Col3, Col2, Col1,......... From TableName"

And also in the Form_Closed event dispose the form instance.

I will try to select the columns in the select statement. But how to close the form instance?

I have the cmdget.dispose and everything at the end of my db connection.

Already tried the code & it worked well. It's just so tedious to re-write the columns one by one. Thanks! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.