Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > datagrid view columns out of order...
 

datagrid view columns out of order...

I have a method that sets up my datagridview. Among important things it does, is setup the column order. I do this by turning off visibility of them all, and then one by one in order turning the visibility on for the ones I want and setting the display order using a var that I increment each time and setting a width... And normally it works. But every now and again, it gets feisty... and start putting things out of order. Unfortunately I didn't notice it fast enough to relate it to some change I made. But the kinds of changes I made were simply to remove column entirely, or add them. And to add additional ones to the list to see.

Anyone know what I could be doing, or what might be going on internally that is messing this up?
Here is the code...

private void dataGridViewAlertsSetup()
{
DataGridView dvg = dataGridViewAlerts;
dvg.DataSource = dataViewAlerts;
foreach (string col in allFields)
dvg.Columns[col].Visible = false;

DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn();
buttonColumn.Name = "descButton";
buttonColumn.HeaderText = "Desc";
buttonColumn.Text = "View";
buttonColumn.UseColumnTextForButtonValue = true;
dvg.Columns.Add(buttonColumn);

DataGridViewButtonColumn idbuttonColumn = new DataGridViewButtonColumn();
idbuttonColumn.DataPropertyName = "id";
idbuttonColumn.UseColumnTextForButtonValue = false;
idbuttonColumn.Name = "idButton";
idbuttonColumn.HeaderText = "ID";
dvg.Columns.Add(idbuttonColumn);

int displayIndex = 0;
columnSetup(dvg, "idButton", true, displayIndex++, 50);
columnSetup(dvg, "Issue", true, displayIndex++, 135);
columnSetup(dvg, "time until", true, displayIndex++, 35);
columnSetup(dvg, "title", true, displayIndex++, 325);
columnSetup(dvg, "owner", true, displayIndex++, 50);
columnSetup(dvg, "priority", true, displayIndex++, 85);
columnSetup(dvg, "submitted_by", true, displayIndex++, 55);
dvg.Columns["submitted_by"].HeaderText = "Filer";
columnSetup(dvg, "descButton", true, displayIndex++, 40);
columnSetup(dvg, "Issue Rank", true, displayIndex++, 20);

dataViewAlerts.Sort = "[Issue Rank] ASC,[Time Until] ASC";
//dvg.Sort(dvg.Columns["Issue Rank"], ListSortDirection.Ascending);
}

private void columnSetup(DataGridView dgv, string colName, bool visible, int displayIndex, int width)
{
DataGridViewColumn dgvC = dgv.Columns[colName];
dgvC.Visible = visible;
dgvC.DisplayIndex = displayIndex;
dgvC.Width = width;
}

The resulting order now is,
ID, Issue, Time Until, Desc, Title, Owner, Filer, Issue Rank, Priority.
It should be
ID, Issue, Time Until, Title, Owner, Priority, Filer, Desc, Issue Rank.

I can't even see a pattern to the madness...

Randell
RandellP  Wednesday, July 29, 2009 10:01 PM
In reading the help topic for displayIndex, a few things popped out:

"The Visible property value does not affect the DisplayIndex value."

and

"Every column in the control has a unique DisplayIndex value. The values start with 0 and proceed in numerical order without skipping any values. When you change the DisplayIndex value for a column, the DisplayIndex values for other columns are changed to reflect the new order. "

Here is the link to the help topic:

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.displayindex.aspx

So it may be that your columns that are not visible are affecting this.

It may be lots easier to just remove all of this code and use the designer. Click on the Grid in the WinForm designer and define your columns here. That way they will show up in the desired sequence.

If you don't know how to use the smart tag, this may help:

http://en.csharp-online.net/Presenting_Data_with_the_DataGridView_Control_in_.NET_2.0%E2%80%94Using_the_Designer_to_Define_Grids


Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Thursday, August 13, 2009 11:48 PM
Interesting.... I can't use the designer because of the way the source gets hooked up and such...
Any idea what is happening behind the scenes when I assign a display index value to a column when another column already has the value?
I assumed that when you assigned something to index 0, all others just bumped up one... and similarly with assigning something to 1...
Clearly I am going to have to do some manual collision handling...
If you don't know, that cool, I can do a bunch of trail and error and eventually figure it out what algorithm is being used... but if you know... that would rock... :)
thanks for the help so far.

Randell
RandellP  Friday, August 14, 2009 12:32 AM
You can still use the designer in this case. You just won't get the drop down items. You will have to type in the information, such as the DataPropertyName, manually. It would still allow you to define the columns in the correct sequence, set up the appropriate column type, and define the mapping to your datatable (through the DataPropertyName).

This may *really* save you a lot of work...


www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Friday, August 14, 2009 3:30 PM
Well, from what I can see, if I have to manually enter the columns into the designer AND keep internal code tracking what columns exist, then if I make a change I have to do it in two places, which I have been told is bad programming practice... so doing it pragmatically 100% is best... And since I have most everything setup... all I need to know is what it is doing when I assign a display index, and I can make my code work with that... just need to know what it is doing. So far I haven't been able to establish the algorithm with my trial and error. It seems not to be anything obvious.

Randell
RandellP  Friday, August 14, 2009 5:48 PM
OK... another idea. If you turn off automatic generation of the columns, you can then build them in your code without worrying about what the system did and what order they were defined in originally.

Might that work?
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Friday, August 14, 2009 5:55 PM
That is interesting and somewhat appealing... But...

I tried reproducing the problem with a new project and simple data... couldn't get it to fail... I tried coping the exact code from the original to a new project, and magically the order was right. The two projects have the same exposed code... Which means something in the auto generated code got messed up and some how is affecting the ordering.

I tried looking at the code in the section for the datagridview in question... but they match too. so back to square one on that...

But clearly since it is working in this other copy, turning off auto-gen isn't needed... but something is going on behind the scenes in the VS generated code...

Randell
RandellP  Friday, August 14, 2009 9:59 PM

You can use google to search for other answers

Custom Search

More Threads

• binding navigator
• Bindingsource.AddNew Question
• Adding Several Tables into a DataSet (WinForms)
• Focus in datagridview
• what will happen if i use command builder
• How to Populate values of dropdown list in a datagrid in C#
• Newbie confused... why cant I update or delete?
• BindingList versus DataGridview.Rows.Add
• Not able to change row color based on the type of data
• DataGridViewComboBoxCell Refresh?