colinreedy674 colinreedy674 - 1 month ago 14
C# Question

Dynamic listview header text

I have this code that will load a bunch of .txt files (when selected from the combobox), and display the data in a listview:

private void cmbFiles_SelectedIndexChanged(object sender, EventArgs e)
{
lvContent.Items.Clear();
lvContent.Columns.Clear();
string[] content = File.ReadAllLines(@"Credentials/" + cmbFiles.SelectedItem);
int colCount = 0;
foreach (string line in content)
{
string[] substrings = line.Split('|');
colCount = Math.Max(colCount, substrings.Count());
ListViewItem listItem1 = new ListViewItem(substrings[0]);
for (int i = 1; i < substrings.Count(); i++)
{
listItem1.SubItems.Add(new ListViewItem.ListViewSubItem(listItem1, substrings[i]));
}
lvContent.Items.Add(listItem1);
}
for (int i = 1; i <= colCount; i++)
lvContent.Columns.Add("");
lvContent.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
}


This works great, but doesn't show any column header text, i'm not sure the best way to display the header text, it will need to change randomly depending on what file is displayed, any tips or views on the best way to go would be appreciated!

Answer

You should change ListView.View Property to Details.

        lvContent.View = View.Details;
Comments