I have a C# WinForm application where I am using a ListView to show what files have been uploaded to my database. I use the same code each time, calling LoadFileAttachments()
when the form loads, and again any time I refresh the list, or add or remove additional attachments from the database. (This part works great)
Where I am having an issue is the GUI side of the ListView. The first time LoadFileAttachments()
runs and fills the ListView, there is a gap between the left side of the ListView and the attachments. On subsequent calls, the gap disappears.
As you can see below, the columns are not changing width, there just seems to be a gap. I tried capturing the MouseClick event of the ListView and using a ListViewHitTestInfo to see what was there, and it is showing the item I am clicking next to, with the property of "Selected = false". Clicking on the icon or text causes the item to be selected, but not in the gap.
What's causing the gap?
Screenshot:
The code I call each time:
private void LoadFileAttachments()
{
attachmentListView.Items.Clear();
ImageList iconList = new ImageList();
attachmentListView.LargeImageList = iconList;
attachmentListView.SmallImageList = iconList;
attachmentListView.StateImageList = iconList;
FileAttachmentInfo[] fileAttach = dbAccess.RetrieveAttachedRecords(loadPNGid.Value);
foreach (FileAttachmentInfo file in fileAttach)
{
ListViewItem item = new ListViewItem(file.FileName);
item.Tag = file.RowID;
iconList.Images.Add(file.FileExtention, ExtractIcons.GetIconImage(file.FileExtention));
item.ImageKey = file.FileExtention;
item.SubItems.Add(GetFileTypeDescriptors.GetFileDescriptor(file.FileExtention));
item.SubItems.Add(Conversions.FileSizeToString(file.FileSize));
item.SubItems.Add(file.DateAdded.ToShortDateString());
attachmentListView.Items.Add(item);
}
if (attachmentListView.Columns.Count == 0)
{
attachmentListView.Columns.Add("Attachment", 150);
attachmentListView.Columns.Add("File type", -2);
attachmentListView.Columns.Add("Size", -2);
attachmentListView.Columns.Add("Date added", -2);
}
}
This is the code in the designer file:
//
// attachmentListView
//
this.attachmentListView.AllowColumnReorder = true;
this.attachmentListView.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.attachmentListView.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
this.attachmentListView.Location = new System.Drawing.Point(0, 0);
this.attachmentListView.MultiSelect = false;
this.attachmentListView.Name = "attachmentListView";
this.attachmentListView.Size = new System.Drawing.Size(440, 301);
this.attachmentListView.TabIndex = 0;
this.attachmentListView.TileSize = new System.Drawing.Size(188, 130);
this.attachmentListView.UseCompatibleStateImageBehavior = false;
this.attachmentListView.View = System.Windows.Forms.View.Details;
this.attachmentListView.DoubleClick += new System.EventHandler(this.attachmentListView_DoubleClick);
this.attachmentListView.MouseClick += new System.Windows.Forms.MouseEventHandler(this.attachmentListView_MouseClick);