array of ListViewItem - how to do it


G

grs

The following is a code example from the Microsoft MSDN.
My question is on the following three lines of code:

ListViewItem item1 = new ListViewItem("item1",0);
ListViewItem item2 = new ListViewItem("item2",1);
ListViewItem item3 = new ListViewItem("item3",0);

You would in most cases know the ListViewItem name or the number of items to
create (item1,item2 etc).
How would you create this individual rows programmatically ?

thanks
grs


=====================================
private void CreateMyListView()
{
// Create a new ListView control.
ListView listView1 = new ListView();
listView1.Bounds = new Rectangle(new Point(10,10), new Size(300,200));

// Set the view to show details.
listView1.View = View.Details;
// Allow the user to edit item text.
listView1.LabelEdit = true;
// Allow the user to rearrange columns.
listView1.AllowColumnReorder = true;
// Display check boxes.
listView1.CheckBoxes = true;
// Select the item and subitems when selection is made.
listView1.FullRowSelect = true;
// Display grid lines.
listView1.GridLines = true;
// Sort the items in the list in ascending order.
listView1.Sorting = SortOrder.Ascending;

// Create three items and three sets of subitems for each item.
ListViewItem item1 = new ListViewItem("item1",0);
// Place a check mark next to the item.
item1.Checked = true;
item1.SubItems.Add("1");
item1.SubItems.Add("2");
item1.SubItems.Add("3");
ListViewItem item2 = new ListViewItem("item2",1);
item2.SubItems.Add("4");
item2.SubItems.Add("5");
item2.SubItems.Add("6");
ListViewItem item3 = new ListViewItem("item3",0);
// Place a check mark next to the item.
item3.Checked = true;
item3.SubItems.Add("7");
item3.SubItems.Add("8");
item3.SubItems.Add("9");

// Create columns for the items and subitems.
listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center);

//Add the items to the ListView.
listView1.Items.AddRange(new ListViewItem[]{item1,item2,item3});

// Create two ImageList objects.
ImageList imageListSmall = new ImageList();
ImageList imageListLarge = new ImageList();

// Initialize the ImageList objects with bitmaps.
imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage1.bmp"));
imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage2.bmp"));
imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage1.bmp"));
imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage2.bmp"));

//Assign the ImageList objects to the ListView.
listView1.LargeImageList = imageListLarge;
listView1.SmallImageList = imageListSmall;

// Add the ListView to the control collection.
this.Controls.Add(listView1);
}
 
Ad

Advertisements

J

Jeffrey Tan[MSFT]

Hi grs,

Thanks for your post.

I do not think I understand you question. What does "How would you create
this individual rows programmatically ?" mean? In your code, I see that you
have added the listviewitems into ListView with listView1.Items.AddRange
method.

Can you show me your concern more concrete?

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

grs

:
Hi Jeffrey,

The problem is the hardcoded "item1". You do not know how many
items there will be so you can not create an ListViewItem object
in advance. I do not think you would want to to a create on
item1,item2, etc and restrict the number of ListViewItems
My question is on the following three lines of code:
ListViewItem item1 = new ListViewItem("item1",0);
---------------^(*) here is the problem
ListViewItem item2 = new ListViewItem("item2",1);

So the recap of my problem is that I do not know the number of rows that
exists how do I create the ListViewItems (which unless I am wrong
correspond to rows)

thanks
grs

grs
 
M

Marcus Andrén

You would in most cases know the ListViewItem name or the number of items to
create (item1,item2 etc).
How would you create this individual rows programmatically ?

First of all, it is usually best to seperate the code for creating the
ListView from the code adding the ListViewItems.

Here is the code I usually use to add ListViewItems. Assuming you have
a collection MyDataClassCollection that contains a number of
MyDataClass instances that can be used to generate ListViewItems.:

private void RefreshListView()
{
listView.BeginUpdate();

listView.Items.Clear();
AddItemsToListView();

listView.EndUpdate();
}


private void AddItemsToListView()
{
//Add to an ArrayList first because AddRange is a lot faster
//than Add when dealing with lots of elements.
//Also, there is no need to turn off the sorter when adding
//all the elements at once

ArrayList listItems = new ArrayList();

foreach (MyDataClass myDataItem in MyDataClassCollection)
{
// The if statement can be removed if all items in
// MyDataClassCollection should be added to the ListView.
if( ShouldDisplay(myDataItem) )
listItems.Add(CreateListViewItem(myDataItem));
}

// Adds the items to the ListView
listView.Items.AddRange(
(ListViewItem[]) listItems.ToArray( typeof(ListViewItem)));
}

// Generate a listviewitem by using the myDataItem instance.
private static ListViewItem CreateEpisodeListViewItem(MyDataClass
myDataItem)
{
ListViewItem item = new ListViewItem(
new string[]
{
myDataItem.StringProperty,
myDataItem.IntProperty.ToString(),
(myDataItem.ReferenceProperty == null) ? "" :
myDataItem.ReferenceProperty.ToString()
});
item.ForeColor = CalculateForeColor(myDataItem);

// It is usually a good idea to use the Tag property to attach
// the MyDataClass instance directly to the ListViewItem.
item.Tag = episode;

return item;
}
 
J

Jeffrey Tan[MSFT]

Hi grs,

Thanks for your feedback.

Sorry, but I still do not understand you very well. I am not sure the
"item1" you mentioned refers to the ListViewItem reference or the first
parameter passed to ListViewItem constructor? Can you clarify this to me?
If you referred to the ListViewItem reference, I do not think this is a
problem, we can reuse the same reference to create many ListViewItem
instances, then add them into ListView.Items collection. The reference need
not correspond to ListViewItem instance one-by-one.
If you referred to the first parameter passed to ListViewItem, I just do
not understand why. This parameter identifies the text to be displayed in
ListView, if you want to add a new item, you SHOULD know which text you
want to display.

In normal situation, once we know the text we want to display in ListView,
we can construct a new ListViewItem instance and add it into ListView.Items
collection, no matter how many items you want to add, only one ListViewItem
reference is needed.

If I misunderstand you, please feel free to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

grs

Jeffrey,

Your statement below cleared everything up for me. I seem to have a
problem with reusing references, I have had this mind road-block before.
Maybe someday I will get it.

thanks for turning me around.
grs
we can reuse the same reference to create many ListViewItem
instances, then add them into ListView.Items collection

grs
 
Ad

Advertisements

J

Jeffrey Tan[MSFT]

I am glad my reply can help you. If you need further help, please feel free
to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top