|
I have a DataGridView on my form that I bind to a BindingSource, which in turn is bound to BindingList<MyStruct>.
MyStruct is a custom struct with three public properties (say A, B, C).
I set up the columns for my DataGridView in code, by adding the appropriate columns in the order in want them (A, B, C). However, at runtime, the grid displays them in the order A, C, B.
Is this a bug? Did I forget anything?
struct MyStruct { private string _a; private string _b; private int _c;
public MyStruct(string a, string b, int c) { _a = a; _b = b; _c = c; }
public string A { get { return _a; } } public string B { get { return _b; } } public int C { get { return _c; } } }
// in the form constructor:
BindingList<MyStruct> list = new BindingList<MyStruct>(); UpdateList(list, someCondition);
bindingSource1.DataSource = list;
dataGridView1.Columns.Add("column0", "A"); dataGridView1.Columns.Add("column1", "B"); dataGridView1.Columns.Add("column2", "C"); dataGridView1.Columns["column0"].DataPropertyName = "A"; dataGridView1.Columns["column1"].DataPropertyName = "B"; dataGridView1.Columns["column2"].DataPropertyName = "C";
dataGridView1.DataSource = bindingSource1; |
| MigrationUser 1 Tuesday, June 07, 2005 4:57 AM |
It seems fine to me... The grid properties in the desginer is all default?
what happned if you change the adding order of the columns? is it always in A C B order ? |
| MigrationUser 1 Tuesday, June 07, 2005 1:01 PM |
Well, I set ReadOnly=true, AllowUserToAddRows=false, AllowUserToDeleteRows=false, MultiSelect=false, SelectionMode=FullRow, and a alternating row cell style. Nothing that should affect the columns...
Changing the adding order doesn't have any effect.
If I don't get this to work, I'll probably have to replace my struct with a typed DataSet. |
| MigrationUser 1 Wednesday, June 08, 2005 3:52 AM |
This is the result of databinding during runtime. Basically the AutoGenerateColumns property defaults to true but if you databind at design time we set the AutoGenerateColumns property to false.
When AutoGenerateColumns is true we will query the datasource for the properties that should be databound and create columns for them. In Beta2 before we add the columns we create we remove any columns that are databound.
Basically just set AutoGenerateColumns to false inside your constructor and you'll be fine. In RTM builds this scenario works.
hope this helps, -mark Program Manager Microsoft This post is provided "as-is"
|
| MigrationUser 1 Wednesday, June 15, 2005 4:29 PM |
I have a similar problem, but I don't bind at runtime. In my scenario, I have a master-detail relationship. My master data is displayed in TextBox objects and my detail data is displayed in a DataGridView. Everything looks fine until I rebuild the application and run it. At this time, all the fields in the detail grid is shuffled around. In other words, whatever order they were put in at design time, is messed up.
The funny thing is, that they are actually not messed up runtime. But when I return to Visual Studio, they are messed up. If I keep running the application, they are still not messed up at runtime. BUT, if I once alter the position of a single column at design time, the columns are now messed up at desing time too.
|
| claesbrandt Saturday, December 10, 2005 9:48 PM |
|
| Ryan Riehle Sunday, September 17, 2006 2:37 PM |
I had the same problem but I was adding the columns with the designer. what I ended up doing was going to where the project is created and deleting the bin and obj folder. I tried everything from adding the fields in the designer to setting autogenerate columns to false in the constructor and this is the only thing that worked for me. |
| CSharpGuy Monday, February 05, 2007 9:19 PM |
Setting autogenerate columns to false in the constructor worked for me
Deleting Bin&Obj didn't work.
1000 thanks I was going crazy.
Camille |
| CamilleLambert Monday, September 01, 2008 2:35 PM |
I have the same problem, the column order is changed the moment I assign a value to the DataSource. I have had this problem with a few DataGrids. Setting/unsetting the frozen property of a column did resolve one of them, but not all.
|
| EddyP Wednesday, June 03, 2009 6:22 AM |
Is there a way around this problem besides setting AutoGenerateColumns to false in the constructor? I need the table to update with different columns based on other user selections. Setting AutoGenerateColumns to false means that the columns don't update when my binding list does. Everything works correctly (appart from the column order!) if it si set to true. |
| kburt Wednesday, July 22, 2009 4:23 PM |