开发者

DataGridView losing initial state specified at design time

开发者 https://www.devze.com 2023-01-26 02:39 出处:网络
When I try to change the datasource of my DataGridView, I am loosing the order of the columns and their type(checkboxes,comboboxes) that i specified at design time.

When I try to change the datasource of my DataGridView, I am loosing the order of the columns and their type(checkboxes,comboboxes) that i specified at design time.

It's like changing the datasource is generating again the datagridview, why ? and How can I change the datasource without losing these information ?

Please if you know give me help

Thanks in advance

On form load i do something like

Private Sub frmGrid_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Path as string
Path="C:\......Myfile.xml"

Dim oData  as dataset
oData = New DataSet
oData.ReadXml(Path)
oData.AcceptChanges() 

DataGridView1.DataSource = oData
DataGridView1.DataMember =  oData.Tables(0).TableName 
end sub

Till now everything is fine the design mode is preserved.

Then I have in my interface I have a button to save the content of my Grid to an excel file (it's an xml, formatted for excel)

Then i want to import this, on a button action, so i do the following

Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click           
Dim MyDs开发者_开发知识库 As DataSet
MyDs = New DataSet
Dim InputFileStream As New System.IO.FileStream(Path, System.IO.FileMode.Open)
MyDs = ImportExcelXML(InputFileStream, True, True)
MyDs.ReadXml(Path)
MyDs.AcceptChanges()
DataGridView1.DataSource = MyDs
DataGridView1.DataMember = MyDs.Tables(0).TableName    
end sub


Probabily you have set up GenerateColums property to "True"

That means the GridView will generate the columns(name and number of them) againts the data sorce.

If the data source (in you case data table) are different it will come up a different grid.

If the datasource are different you can design the layout of your gridView but after that you have to manually bind the data to the columns as you wish.You can do that into

RowDataBound event.


The datatable you are trying to assign to the DataGridView1.DataSource is structured different from the way you defined the DataGridView at design time. Put a break point in your code right before you reassign the new DataTable to the DataSource and see what is different in the structure. It is most likely that you either have more or less DataColumns in place, or the columns could be in a different order. They might even be named different. If at design time you set your DataGridViewColumn's DataProperty to a specific name, than then name needs to be exact.

You'll just have to check it at that breakpoint and see what the differences are.


I found a solution, your hints helped me to look on the right direction to find a solution !

What i was doing was on form load

MaGrille.DataSource = oData
MaGrille.DataMember = oData.Tables(0).TableName

Then in the button click I do :

MaGrille.DataSource = MyDs
MaGrille.DataMember = MyDs.Tables(0).TableName

The problem is that the grid is rebuild and i loose the formatting, column order etc..

After many tests, I understood that It wasn't working because the names of datatables are differents ! I don't understand why, but it was the error as it seems. I had the following values oData.Tables(0) = "DECLARATION" MyDs.Tables(0) = "Sheet1"

So I noticed that if I rename MyDs.Tables(0) by "DECLARATION" instead of "Sheet1", and i click on the button, this time I don't loose formatting, neither orders ...

This inspired me to find the SOLUTION :

After many tries, I realized that if instead of doing on form load

MaGrille.DataSource = oData
MaGrille.DataMember = oData.Tables(0).TableName

and on button click :

MaGrille.DataSource = MyDs
MaGrille.DataMember = MyDs.Tables(0).TableName

I do :

on form load

MaGrille.DataSource = oData.Tables(0)

and on button click

MaGrille.DataSource = MyDs.Tables(0)

All works fine even if the oData.Tables(0).TableName is different from MyDs.Tables(0).TableName !!

I found the solution Now, but still can't understand the WHY If anyone get an explication, please let me know.

Thanks to all again

0

精彩评论

暂无评论...
验证码 换一张
取 消