如何在一列中填充listview [英] How to populate listview in one column
问题描述
下面如果我用来填充ListView的代码:
Below if the code I used to populate a ListView:
foreach (DataRow dr in dt_Updated.Rows) {
ListViewItem lvi = new ListViewItem(dr[eGisId].ToString());
listViewID.Items.Add(lvi);
}
listViewID.Items[0].Selected = listViewID.Items[0].Focused = true;
listViewID.View = System.Windows.Forms.View.List;
但是,如果有很多列,则会在多列上填充数据。我希望数据只填充在一列中,同时显示垂直滚动条。
参考如何将垂直ScrollBar的属性设置为Ultralistview控件--Windows窗体 - WinListView [ ^ ]
However, the data are populated on multiple columns if there are many. I want the data are populated only in one column, and meanwhile make the vertical scrollbar displayed.
Referring to How to Set the property of vertical ScrollBar to Ultralistview control - Windows Forms - WinListView[^] in which the user use
"myList".ViewSettingsList.MultiColumn = false;
不幸的是,对于ListView控件,我找不到它的ViewSettingsList属性。
这样做的正确方法是什么?我希望有人可以提供他/她的答案。
此外,在数据显示后,我希望第一项用默认背景颜色突出显示。
Unfortunately, for the ListView control, I could not find its ViewSettingsList property.
What's the proper way to do it? I wish someone can provide his/her answer.
Besides, after the data displays, I want to the 1st item is highlighted with the default background color.
listViewID.Items[0].Selected = true;
但是目前,所选项目显示为不突出显示。我该怎么办?
谢谢你能提供帮助。
我的尝试:
尝试在一列中填充ListView但不成功
But currently, the selected item displays like to be not highlighted. What I should do on this?
Thanks if you can help.
What I have tried:
Tried Populate ListView in one Column but not successful
推荐答案
如果你在谈论WinForm ListView [ ^ ] ...
If you're talking about WinForm ListView[^]...
//add single column
//-2 => autosize
listViewID.Columns.Add("MyColumn", -2, HorizontalAlignment.Left);
listViewID.FullRowSelect = true;
listViewID.GridLines = true;
listViewID.View = System.Windows.Forms.View.List;
//load data
foreach (DataRow dr in dt_Updated.Rows) {
ListViewItem lvi = new ListViewItem(dr[eGisId].ToString());
listViewID.Items.Add(lvi);
}
//move focus to listview first
listViewID.Focus();
listViewID.Items[0].Selected = true;
详情请见:关于列表视图控件(Windows) [ ^ ]
ListViewItem.Selected属性(System.Windows.Forms) [ ^ ]
参考 c#解决问题 - 使列表视图在垂直方向滚动 - 堆栈溢出 [ ^ ]。即,我添加了以下代码:
Problem solved by referring to c# - Making List view scroll in vertical direction - Stack Overflow[^]. Namely, I added the following code:
// Add a dummy column
ColumnHeader header = new ColumnHeader();
header.Text = "";
header.Name = "col1";
listViewID.Columns.Add(header);
// Then
listViewID.Scrollable = true;
listViewID.View = System.Windows.Forms.View.Details;
感谢您的观点。
Thanks for your view.
这篇关于如何在一列中填充listview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!