DataGridView w/ List - order of columns

J

jeffc

I've got a DataGridView and set the DataSource to a list I've created. The
problem is, the columns are not showing in the order I'd like. I cna't
figure out how to change the order. The physical order of the fields in the
class that the list holds seems to make no difference.
 
C

Chris Shepherd

jeffc said:
I've got a DataGridView and set the DataSource to a list I've created. The
problem is, the columns are not showing in the order I'd like. I cna't
figure out how to change the order. The physical order of the fields in the
class that the list holds seems to make no difference.

Try setting the DisplayIndex property of the columns in
DataGridView.Columns.

ie:

DataGridView grid = new DataGridView();

grid.DataSource = yourNiftyListObject;

grid.Columns[4].DisplayIndex = 0;
grid.Columns[2].DisplayIndex = 1;
grid.Columns[1].DisplayIndex = 2;
grid.Columns[3].DisplayIndex = 3;
grid.Columns[0].DisplayIndex = 4;


Chris.
 
J

jeffc

Chris Shepherd said:
Try setting the DisplayIndex property of the columns in
DataGridView.Columns.

Yup, that's what I was looking for. Thanks! *Almost* worked, BUT now
columns 3 and 4 are switched, and I can't get them in order. I have 6
columns, and columns 0, 1, 2, and 5 are all in the right place. But it
swaps 3 and 4 on me. Any ideas?
 
C

Chris Shepherd

jeffc said:
Yup, that's what I was looking for. Thanks! *Almost* worked, BUT now
columns 3 and 4 are switched, and I can't get them in order. I have 6
columns, and columns 0, 1, 2, and 5 are all in the right place. But it
swaps 3 and 4 on me. Any ideas?

Nope, I'm having the exact same issue (which I reported only to
microsoft.public.dotnet.windowsforms). There's clearly something I don't
get about the order of the columns or the order in which the
DisplayIndex is applied, but I've received no response to my other thread.

One thing that could work temporarily is to assign an extra value
between 3 and 4 (ie: 4 becomes 5, 5 becomes 6, etc.).

Chris.
 
P

Peter Duniho

jeffc said:
Yup, that's what I was looking for. Thanks! *Almost* worked, BUT now
columns 3 and 4 are switched, and I can't get them in order. I have 6
columns, and columns 0, 1, 2, and 5 are all in the right place. But it
swaps 3 and 4 on me. Any ideas?

Without you posting a concise-but-complete example of code that reliably
reproduces the problem, it's hard to know for sure what the problem is.
However, keep in mind that when you set the DisplayIndex for a given
column, the other columns display index is renumbered to accomodate that.

So, it's important to make sure you set the DisplayIndex properties in
increasing order, to ensure that you don't disturb the DisplayIndex for
columns you've already set explicitly.

Pete
 
J

jeffc

Chris Shepherd said:
Nope, I'm having the exact same issue (which I reported only to
microsoft.public.dotnet.windowsforms). There's clearly something I don't
get about the order of the columns or the order in which the DisplayIndex
is applied, but I've received no response to my other thread.

At least I know a new forum to post to next time. This is so weird - if I
assign 0 to the offending column, it goes to column 0. If I give it any
other number, it goes directly to column 3.
 
J

jeffc

Peter Duniho said:
Without you posting a concise-but-complete example of code that reliably
reproduces the problem, it's hard to know for sure what the problem is.
However, keep in mind that when you set the DisplayIndex for a given
column, the other columns display index is renumbered to accomodate that.

So, it's important to make sure you set the DisplayIndex properties in
increasing order, to ensure that you don't disturb the DisplayIndex for
columns you've already set explicitly.

Yeah, that makes sense. It's pretty simple though. I won't show the rest
of the code because so much seems to be working, I don't understand what
could be causing the problem, and the code is simple. The index numbering
is:
DataGridView_VersionList.Columns["FileName"].DisplayIndex = 0;

DataGridView_VersionList.Columns["ProductVersion"].DisplayIndex = 1;

DataGridView_VersionList.Columns["FileVersion"].DisplayIndex = 2;

DataGridView_VersionList.Columns["FileDescription"].DisplayIndex = 3;

DataGridView_VersionList.Columns["Comments"].DisplayIndex = 4;

DataGridView_VersionList.Columns["SpecialBuild"].DisplayIndex = 5;

If I change the display index for FileVersion to 0, it goes to the first
column. If I set it to any other number (adjusting others accordingly, as
you mentioned), it always goes to index 3, and FileDescription slips ahead
of it. Actually there are other columns with the same problem. It's as if
it does what it wants past index 1.
 
P

Peter Duniho

jeffc said:
Yeah, that makes sense. It's pretty simple though. I won't show the rest
of the code because so much seems to be working, I don't understand what
could be causing the problem, and the code is simple.

You will get better assistance if you do post a concise-but-complete
code example that reliably reproduces the problem.

What you posted _should_ work, IMHO. And the reply from Chris does
suggest that there may in fact be a bug in the DataGridViewColumn that's
preventing this from working correctly.

But I know that I didn't run into the problem the one time I had to
reorder columns in my own DataGridView, and so to rule out the
possibility that there's something else in your code confounding the
DisplayIndex setting, you should reduce the code to the bare minimum
required to reproduce the issue and post that here.

The code should compile with minimal extra effort on someone else's
part. A complete copy of the Designer-generated code along with your
own custom code in a single file that can be pasted into an empty
project is best. Obviously this means that if you are binding the grid
to some data source or something, you need to hardcode some kind of data
source or initialization for the DataGridView as an alternative, since
we don't have a copy of your database.

And did I mention it should be the bare minimum required to reproduce
the issue? :)

Pete
 
N

Nico Baumgarten

I do have the exact same problem. In my case I have 3 DataGridViews on a
form and only one of them is showing this very strange behaviour!
Even more obscure is that if I remove the datasource and bind it to a
new one (although with same layout) it sometimes even reorders the
columns again, or deletes my column definitions and actually display
columns, which were set to not visible!
My DatagridView is configured solely through the desinger and I have
noticed the following behaviour in testing:

1. If I dont bind a datasource the columns are displayed in correct
order the way they should.
2. If I bind a datasource, that has its column names changed (e.g.
original Column--> Column1) it shows first the columns defined for the
datagridview in the designer (empty though) followed by the columns of
the datasource
3. If I bind a datasource and have all columns names (as in 2.) renamed
but one it works correctly as described in 2.
4.If I bind a datasource and have more than one column actually match
the definition (as described in 2.) the strange behaviour described
above begins.

So here I am stuck - hoping for some ideas for a solution or
workaround?!

Regards

Nico
 
P

Peter Duniho

Nico said:
[...]
So here I am stuck - hoping for some ideas for a solution or
workaround?!

Looking at all three complaints together, and seeing that it appears
that in all three cases, one common factor is that the DataGridView is
being bound to a data source, I am wondering if this is related to the
difference between a columns title text and its name.

In particular, when you index the column by name, you're looking it up
by the Name property. What you see on the screen is the HeaderText
property.

Is it possible that because of the data binding, the Name property is
getting set to something other than what you expect, and as a result the
assignment of DisplayIndex isn't happening as you expect?

It would be nice if at least one of the three of you who have this
problem could double-check that, if for no other purpose than to rule it
out as a possible culprit here. Please report back your findings, one
way or the other.

Thanks,
Pete
 
J

jeffc

Peter Duniho said:
In particular, when you index the column by name, you're looking it up by
the Name property. What you see on the screen is the HeaderText property.

Is it possible that because of the data binding, the Name property is
getting set to something other than what you expect, and as a result the
assignment of DisplayIndex isn't happening as you expect?

It would be nice if at least one of the three of you who have this problem
could double-check that, if for no other purpose than to rule it out as a
possible culprit here. Please report back your findings, one way or the
other.

In my case it seems the Name property and HeaderText property are exactly
the same for every column.
 
J

Joaquin Negron

Don't know if you finally found a solution to this problem, but here it goes, I was having this same problem in one of my grids which began life as a copy of another one. On the new grid, however, I had initially added 2 columns using the grid's column collection property. I then removed them and started loading the columns from the database. That's when the problem started. I fixed it by copying and pasting the grid from the original datagrid. So, I would say this is bug in the grid that occurs, at least in this case, if you use the columns collection and then removed them. It appears something is not reset correctly when the columns are removed. The work around appear to be to remove the grid and re-add a new one. Well, it worked for me, not an ideal solution but I hope it helps someone.
I've got a DataGridView and set the DataSource to a list I've created. The
problem is, the columns are not showing in the order I'd like. I cna't
figure out how to change the order. The physical order of the fields in the
class that the list holds seems to make no difference.
Try setting the DisplayIndex property of the columns in
DataGridView.Columns.

ie:

DataGridView grid = new DataGridView();

grid.DataSource = yourNiftyListObject;

grid.Columns[4].DisplayIndex = 0;
grid.Columns[2].DisplayIndex = 1;
grid.Columns[1].DisplayIndex = 2;
grid.Columns[3].DisplayIndex = 3;
grid.Columns[0].DisplayIndex = 4;


Chris.
On Wednesday, August 08, 2007 4:59 PM jeffc wrote:

Yeah, that makes sense. It's pretty simple though. I won't show the rest
of the code because so much seems to be working, I don't understand what
could be causing the problem, and the code is simple. The index numbering
is:
DataGridView_VersionList.Columns["FileName"].DisplayIndex = 0;

DataGridView_VersionList.Columns["ProductVersion"].DisplayIndex = 1;

DataGridView_VersionList.Columns["FileVersion"].DisplayIndex = 2;

DataGridView_VersionList.Columns["FileDescription"].DisplayIndex = 3;

DataGridView_VersionList.Columns["Comments"].DisplayIndex = 4;

DataGridView_VersionList.Columns["SpecialBuild"].DisplayIndex = 5;

If I change the display index for FileVersion to 0, it goes to the first
column. If I set it to any other number (adjusting others accordingly, as
you mentioned), it always goes to index 3, and FileDescription slips ahead
of it. Actually there are other columns with the same problem. It's as if
it does what it wants past index 1.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top