Reputation: 932
I have this:
lvPalette.Columns.Add("Code", -2, HorizontalAlignment.Center);
lvPalette.Columns.Add("Attendu", -2, HorizontalAlignment.Center);
lvPalette.Columns.Add("Chargé", -2, HorizontalAlignment.Center);
lvPalette.Columns.Add("Validé", -2, HorizontalAlignment.Center);
lvPalette.Columns.Add("Description", -2, HorizontalAlignment.Center);
Which end up making the first column as large as the control itself instead to be just as large as the header text. Why is that?
I tried to set first column widt to 100, but then its the 2nd column that take all the space up to the control width.
EDIT:I think I figured why it does that. It seems that setting width to -2 force the header to take whole control width space avaible. So when first column is added, it is the only one existing, so it take the whole control space, then the others are added and set to correct size.
So question would be, how can I prevent it to do it?
Upvotes: 1
Views: 914
Reputation: 61
Based on your observation and my experience with the sizing applied to the first column, I added a throw away column first and removed when I added the second column. The second column is the one to keep. I then did a Remove for the first column and added the rest of the valid ones. What do you know, the sizing was fine!
if (!pastTossOutCol) {
listView1.Columns.RemoveAt( 0 );
pastTossOutCol = true;
}
Maybe it will help someone else who spent hours trying to make it work.
Upvotes: 2
Reputation: 932
It seems there is no way to prevent this behavior beside setting values.
I found a way with autorizing via header and fields then selecting the one that had the largest width, but it has the bad side that it force 3 resizes, which is not graphically nice.
Upvotes: 0
Reputation: 932
I think I figured why it does that. It seems that setting width to -2 force the header to take whole control width space avaible. So when first column is added, it is the only one existing, so it take the whole control space, then the others are added and set to correct size.
So question would be, how can I prevent it to do it?
Upvotes: 0
Reputation: 7986
Try calling the AutoResize method on each of the columns after you've added the columns to the listview.
Upvotes: 0