简体   繁体   中英

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!

您应该将ListView.View属性更改为Details。

        lvContent.View = View.Details;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM