如何去除的ListView C#列 [英] How to remove columns from ListView c#
问题描述
我是新与的ListView
,并遇到了一个小问题:
I'm new with ListView
and encountered a small problem :
我有一个的ListView
在的WinForm
这是我填满它具有很多列(约15)线后线,后来我有一个选项,以节省的ListView
到 CSV
。
我想要的是一种让用户从的ListView
删除不需要的列。
I have a ListView
in WinForm
which I fill it line after line with many columns (about 15) , later on I have an option to save ListView
to CSV
.
what I want is a way to let the user delete unwanted columns from 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
{
//?
}
}
后来我加行像
later I add line like
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(名称)
What i need is a way to get all names of columns and than delete columns by name or some kind of IndexOf(name)
请点我朝着正确的方向
修改
只是clearfy我不隐藏列(设置宽度= 0),我想删除这些
,来帮助你了解我的观点我已经添加我的方式保存的ListView
我的方式保存到CSV:
just to clearfy I dont to hide columns (settings width = 0) I want to delete them
to help you understand my point I've added the way I save the ListView
the way I save to 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
推荐答案
您可以通过名称引用该列。
You can reference the column by name.
var columnToRemove = ResultsLogTab_ListView.Columns["Name Of Column"];
然后删除
and then remove it
ResultsLogTab_ListView.Columns.Remove(columnToRemove);
这是所有记录上的 MSDN 这是一个很好的资源。
This is all documented on MSDN which is a great resource.
从CSV关于保存/载入假设你有一个列表<串>
列名(从获得的说CSV文件)你想保留,那么你可以使用LINQ发现不匹配,然后对结果进行迭代,以删除这些列。
With regards to saving/loading from CSV let's say you have a List<string>
of column names (obtained from said CSV file) you want to keep then you can use LINQ to find the columns that do not match, and then iterate over the result to remove them.
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);
}
这篇关于如何去除的ListView C#列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!