ListView control problem.

  • Thread starter Thread starter Andrew
  • Start date Start date
A

Andrew

Hello, friends,

I know this sounds stupid, but I could not figure it out. So, help please.

I have ListView control in Details view and I need to populate. I used the
following code

for (int count = 1; count <= 50; count++)
{
ListViewItem listViewItem = new ListViewItem();
listViewItem.Text = "Page " + count.ToString();
listViewItem.Tag = count.ToString();
this.ListViewPageNumber.Items.Add(listViewItem);
}

However, to my surprise, I saw NOTHING in the list box, except the vertical
scroll bar was displayed.

Any ideas?
 
You need to define column headers for the listview control to display in
details mode. You can do this at design time by adding to the listview
control's columns collection, or you can modify your code to look like this:


ColumnHeader ch = new ColumnHeader();
ch.Text = "Page Number";
this.ListViewPageNumber.Columns.Add(ch);

for (int count = 1; count <= 50; count++)
{
ListViewItem listViewItem = new ListViewItem();
listViewItem.Text = "Page " + count.ToString();
listViewItem.Tag = count.ToString();
this.ListViewPageNumber.Items.Add(listViewItem);
}

RappyFood
 
Thank, Rappy Food, it worked.

But, when I tried to select all the items using with HideSelection = false:

private void ButtonSelectAll_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.ListViewPageNumber.Items.Count; i++)
{
this.ListViewPageNumber.Items.Selected = true;
}
}

I could NOt see the items being selected. This control is really weired.

Any ideas? Thanks.
 
Interesting

It seems that you can only see the selected items when the control itself is
selected.
I added one line to your code, shifting focus to the listview control.
I think that it is possible to see the selected items when the control does
not have the focus, but I haven't figured it out yet.

[vc#]
private void ButtonSelectAll_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.ListViewPageNumber.Items.Count; i++)
{
this.ListViewPageNumber.Items.Selected = true;
}
this.ListViewPageNumber.Select();
}
[/vc#]


Sorry about the delay. I tend to get distracted.

RappyFood


Andrew said:
Thank, Rappy Food, it worked.

But, when I tried to select all the items using with HideSelection =
false:

private void ButtonSelectAll_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.ListViewPageNumber.Items.Count; i++)
{
this.ListViewPageNumber.Items.Selected = true;
}
}

I could NOt see the items being selected. This control is really weired.

Any ideas? Thanks.


RappyFood said:
You need to define column headers for the listview control to display in
details mode. You can do this at design time by adding to the listview
control's columns collection, or you can modify your code to look like
this:


ColumnHeader ch = new ColumnHeader();
ch.Text = "Page Number";
this.ListViewPageNumber.Columns.Add(ch);

for (int count = 1; count <= 50; count++)
{
ListViewItem listViewItem = new ListViewItem();
listViewItem.Text = "Page " + count.ToString();
listViewItem.Tag = count.ToString();
this.ListViewPageNumber.Items.Add(listViewItem);
}

RappyFood
 
Back
Top