I have a listview object that I want to have 2 columns. I want the two columns to stretch across the width of the listview and to have both column headers centered in each column. If I set the size of the columns to -2 (auto size), I get the following result (code followed by screenshot):
this.recipList.Location = new System.Drawing.Point(16, 32);
this.recipList.Name = "recipList";
this.recipList.Size = new System.Drawing.Size(376, 296);
this.recipList.TabIndex = 1;
this.recipList.UseCompatibleStateImageBehavior = false;
this.recipList.View = System.Windows.Forms.View.Details;
this.recipList.Columns.Add("Recipient", -2, System.Windows.Forms.HorizontalAlignment.Center);
this.recipList.Columns.Add("Number of Reports", -2, System.Windows.Forms.HorizontalAlignment.Center);
If I drag the columns manually within this window to mimic what I WANT to have, I get this. the only issue here is that even with Recipient
set to HorizontalAlignment.Center
it still left-aligns after I drag it to where I want it:
At this point I figure that the way to get both columns to span across the entire width of the listview box and for each column to take up half of it, I simply need to set the size of each column to 1/2 of the width of the entire listview. So I do this:
this.recipList.Location = new System.Drawing.Point(16, 32);
this.recipList.Name = "recipList";
this.recipList.Size = new System.Drawing.Size(376, 296);
this.recipList.TabIndex = 1;
this.recipList.UseCompatibleStateImageBehavior = false;
this.recipList.View = System.Windows.Forms.View.Details;
this.recipList.Columns.Add("Recipient", 188, System.Windows.Forms.HorizontalAlignment.Center);
this.recipList.Columns.Add("Number of Reports", 188, System.Windows.Forms.HorizontalAlignment.Center);
Since 188 is 1/2 of 376. This produces the following result:
As you can see, it almost worked with 2 issues.
- The
Number of Reports
column spans off of the listview box and creates a scrollbar. I don't want this. - Recipient is still left-aligned even though it is set to
HorizontalAlignment.Center
.
Is there a better way to approach these two issues?