在 ObjectListView C# 中生成列
在普通的 ListView 中,我通常会动态生成列,如下所示:
var list = new List<ColumnNameSize>();
list.Add(new ColumnNameSize("Computer Name", 0));
list.Add(new ColumnNameSize("Full Computer Name", 150));
list.Add(new ColumnNameSize("Migrated", 150));
list.Add(new ColumnNameSize("Disabled", 0));
list.Add(new ColumnNameSize("Disabled & Not Migrated", 150));
generateListView(listViewObjects, list);
public static void generateListView(ListView varListView, List<ColumnNameSize> nameSizeList) {
varListView.ClearColumns();
foreach(ColumnNameSize nameSize in nameSizeList) {
varListView.AddColumn(nameSize.ColumnName, nameSize.ColumnSize);
}
}
How will I do it in ObjectListView。我尝试了多种组合:
public void generateListView(ObjectListView varListView, List<ColumnNameSize> nameSizeList) {
ArrayList myArrayList = new ArrayList();
foreach (ColumnNameSize nameSize in nameSizeList) {
OLVColumn columnHeader = new BrightIdeasSoftware.OLVColumn();
columnHeader.Width = nameSize.ColumnSize;
columnHeader.Text = nameSize.ColumnName;
myArrayList.Add(columnHeader);
varListView.AllColumns.Add(columnHeader);
}
Object columnsHeader = myArrayList.ToArray();
varListView.Columns.AddRange( columnsHeader);
}
但似乎都不起作用......
In normal ListView I usually generate columns on fly with something like:
var list = new List<ColumnNameSize>();
list.Add(new ColumnNameSize("Computer Name", 0));
list.Add(new ColumnNameSize("Full Computer Name", 150));
list.Add(new ColumnNameSize("Migrated", 150));
list.Add(new ColumnNameSize("Disabled", 0));
list.Add(new ColumnNameSize("Disabled & Not Migrated", 150));
generateListView(listViewObjects, list);
public static void generateListView(ListView varListView, List<ColumnNameSize> nameSizeList) {
varListView.ClearColumns();
foreach(ColumnNameSize nameSize in nameSizeList) {
varListView.AddColumn(nameSize.ColumnName, nameSize.ColumnSize);
}
}
How would I do it in ObjectListView. I've tried multiple combinations:
public void generateListView(ObjectListView varListView, List<ColumnNameSize> nameSizeList) {
ArrayList myArrayList = new ArrayList();
foreach (ColumnNameSize nameSize in nameSizeList) {
OLVColumn columnHeader = new BrightIdeasSoftware.OLVColumn();
columnHeader.Width = nameSize.ColumnSize;
columnHeader.Text = nameSize.ColumnName;
myArrayList.Add(columnHeader);
varListView.AllColumns.Add(columnHeader);
}
Object columnsHeader = myArrayList.ToArray();
varListView.Columns.AddRange( columnsHeader);
}
But none seems to work...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你能试试这个吗?
Can you try this?
要通过代码手动添加列后使其可见,您需要使用以下行重建它们:
根据 Bala 的回答,您将获得以下代码:
To make the columns visible after adding them manually through code, you need to rebuild them with the following line:
Based on Bala's answer, you get the following code:
为了使 ObjectListView 按预期工作,您必须将模型对象所需属性的名称定义为 AspectName。
我不想首先创建 ColumnNameSize 列表,而是一一添加列:
For the ObjectListView to work as intended, you have to define the name of the desired property of your model object as AspectName.
Instead of first creating a list of ColumnNameSize, I'd rather add the columns one by one: