Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below if the code I used to populate a ListView:
C#
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;

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
C#
"myList".ViewSettingsList.MultiColumn = false;

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.
C#
listViewID.Items[0].Selected = true;

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
Posted
Updated 24-May-17 7:38am
v2
Comments
Maciej Los 24-May-17 13:17pm    
What framework: WinForm, WPF, ... ?

If you're talking about WinForm ListView[^]...

C#
//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;


For further details, please see: About List-View Controls (Windows)[^]
ListViewItem.Selected Property (System.Windows.Forms)[^]
 
Share this answer
 
Comments
s yu 24-May-17 14:09pm    
Thanks.
Maciej Los 24-May-17 14:13pm    
You're very welcome.
Problem solved by referring to c# - Making List view scroll in vertical direction - Stack Overflow[^]. Namely, I added the following code:
C#
// 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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900