Preserve DataGridView Columns

mcardia

New member
Joined
Sep 1, 2011
Messages
3
Programming Experience
10+
Hi,


I´m migrating a project from VB6 to VB.NET 2010 Express.
I´m replacing msflex grid with datagridview. But i´m not quite familiar with it.
I´m not using datasource. This will be done (far) latter.


So far i´m filling datagridview by code reading a recordset from mdb. Works fine.


The problem is that, I have defined the columns at visual studio designer mode, without using any code.
When I open my mdi child form for the first time it works. But If I close this form and open it again a error occurs because it loses the defined columns.


Any idea about how to preserve columns defined at design after close de form?
 
DataGridViews don't just spontaneously lose columns. You don't have to do anything to preserve them. If columns are being removed it is because you are removing them.

Regardless, why would you bother doing all that hard work when it is far simpler and quicker to use data-binding. Create a DataAdapter, call Fill to populate a DataTable and bind it to the grid. It can be as few as five lines, depending on the circumstances, e.g.
Using adapter As New OleDbDataAdapter("SQL query here", "connection string here")
    Dim table As New DataTable

    adapter.Fill(table)
    myDataGridView.DataSource = table
End Using
 
The only "clear" line i have in the entire project for this grid is

GridClientes.Rows.Clear()

I already tried to comment this line, but error still happens.

May this be something related to mdi child form? The error occurs only when I open the window form for de second time. The first time works fine.

About using ole adapter. I will. but not now. I must focus to convert from vb6 now and optimize the code later.

[]´s
 
As I said, columns don't just disappear on their own. Either something is corrupt or something that you're doing is removing them. Unfortunately there's no way for us to know which or what. It's situations like this where you need to do some testing, which is a big part of what software development is. Create a new form, add a DataGridView, add some columns and then show the form twice. Do the columns disappear? If not then you know that there's something wrong somewhere with your original form or how you're using it.
 
VB.NET:
Private Sub btnClientes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClientes.Click
Frm_Consulta_Cliente.MdiParent = Me
Frm_Consulta_Cliente.Show()
End Sub


There is no code for load event or close/dispose event.
 
Back
Top