【问题标题】:How to remove columns from ListView c#如何从 ListView c# 中删除列
【发布时间】:2014-09-17 07:12:31
【问题描述】:

我是ListView 的新手,遇到了一个小问题:

我有一个 ListViewin WinForm ,我用许多列(大约 15 列)逐行填充它,稍后我可以选择将 ListView 保存到 CSV 。 我想要的是一种让用户从ListView 中删除不需要的列的方法。

我首先创建列的功能

    private void checkAddListBoxColumns(DomainLayer.ScriptLogic.session sessionToAdd)
    {
        if (IsResultLogListViewFirstRun)
        {
            IsResultLogListViewFirstRun = false;
            this.Invoke((MethodInvoker)delegate
            {
                ResultsLogTab_ListView.Columns.Clear();
            });

            foreach (var item in sessionToAdd.comments.Keys)
            {
                this.Invoke((MethodInvoker)delegate
                {
                    ResultsLogTab_ListView.Columns.Add(item);
                });
            }
            McsTableRow mcsTR = new McsTableRow();
            Type t = mcsTR.GetType();
            foreach (/*PropertyInfo*/FieldInfo info in t.GetFields())
            {
                this.Invoke((MethodInvoker)delegate
                {
                    ResultsLogTab_ListView.Columns.Add(info.Name+" 1");
                });
            }
            foreach (FieldInfo info in t.GetFields())
            {
                this.Invoke((MethodInvoker)delegate
                {
                    ResultsLogTab_ListView.Columns.Add(info.Name+" 2");
                });
            }
            this.Invoke((MethodInvoker)delegate
            {
                ResultsLogTab_ListView.Columns.Add("Effective PHY Rate");
            });
            //...more of the same
        }
        else
        {
            //?
        }
    }

稍后我添加类似

的行
            private OnSessionToAdd()
            {
            foreach (string key in sessionToAdd.comments.Keys)
            {
                item.SubItems.Add(sessionToAdd.comments[key]);
            }

            Type t = sessionToAdd.CurrentMCSCheck.firstMcsRow.GetType();

            foreach (FieldInfo info in t.GetFields())
            {
                string x = info.GetValue(sessionToAdd.CurrentMCSCheck.firstMcsRow) as string;
                item.SubItems.Add(x);
            }

            t = sessionToAdd.CurrentMCSCheck.secondMcsRow.GetType();

            foreach (FieldInfo info in t.GetFields())
            {
                string x = info.GetValue(sessionToAdd.CurrentMCSCheck.secondMcsRow) as string;
                item.SubItems.Add(x);
            }

            item.SubItems.Add(sessionToAdd.CurrentMCSCheck.EffectivePHYRate);

            this.Invoke((MethodInvoker)delegate
            {
             ResultsLogTab_ListView.Items.Add(item);
            });
            }

我需要的是一种获取所有列名的方法,而不是按名称或某种 IndexOf(name) 删除列

请指出正确的方向

编辑

只是为了清除我不隐藏列(设置宽度 = 0)我想删除它们 为了帮助您理解我的观点,我添加了保存ListView 的方式 我保存到 CSV 的方式:

public StringBuilder ListViewToCSV(ListView listView, string filePath, bool includeHidden)
{
    //make header string
    StringBuilder result = new StringBuilder();
    WriteCSVRow(result, listView.Columns.Count, i => includeHidden || listView.Columns[i].Width > 0, i => listView.Columns[i].Text);

    //export data rows
    foreach (ListViewItem listItem in listView.Items)
        WriteCSVRow(result, listView.Columns.Count, i => includeHidden || listView.Columns[i].Width > 0, i => listItem.SubItems[i].Text);

    return result;

}

private static void WriteCSVRow(StringBuilder result, int itemsCount, Func<int, bool> isColumnNeeded, Func<int, string> columnValue)
{
    bool isFirstTime = true;
    for (int i = 0; i < itemsCount; i++)
    {
        if (!isColumnNeeded(i))
            continue;

        if (!isFirstTime)
            result.Append(",");
        isFirstTime = false;

        result.Append(String.Format("\"{0}\"", columnValue(i)));
    }
    result.AppendLine();
}

不仅仅是保存StringBuilder

【问题讨论】:

  • 这是 WPF 吗? WinForms?
  • soory ,这是winform --> 编辑问题
  • 有史以来最好的问题 :)

标签: c# winforms listview


【解决方案1】:

您可以按名称引用列。

var columnToRemove = ResultsLogTab_ListView.Columns["Name Of Column"];

然后删除它

ResultsLogTab_ListView.Columns.Remove(columnToRemove);

这一切都记录在MSDN 上,这是一个很好的资源。

更新

关于从 CSV 保存/加载,假设您有一个 List&lt;string&gt; 的列名(从上述 CSV 文件中获得)您想要保留,那么您可以使用 LINQ 查找不匹配的列,然后进行迭代在结果上删除它们。

using System.Linq;

...

var columnNames = new List<string> { "one", "two", "three" };

var columnsToDelete = ResultsLogTab_ListView.Columns.Where(c => !columnNames.Contains(c.Name));

foreach(var column in columnsToDelete)
{
     ResultsLogTab_ListView.Columns.Remove(column);
}

【讨论】:

  • +1 你的更新很棒!!你能告诉我如何获取 ListView 列的名称
  • 固定链接。您是指ListView 的列名还是CSV 中的名称?
  • ListView 中列的名称 --> 我想我有一个答案:stackoverflow.com/questions/14985240/…
  • ColumnHeader 有一个名为 Name 的属性。这又是here on MSDN
  • 感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-18
  • 2018-11-08
  • 2018-09-10
相关资源
最近更新 更多