Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populating a listview

Status
Not open for further replies.

JungleMonkey

Programmer
Aug 18, 2002
19
0
0
US
Hello,

I am very new to C#. I am using a listview control. Here is my question. How do I access individual columns in the listview. I created two columns while in design mode.
Below is a picture of what I have thus far. I have populated column 1. Even though column two is created, I cannot access the cells in column 2. Please help!
col 1 col 2
100 <access to this cell>
200
300
400

Thank you in advance
 
Well, here is a little snippet of code that perhaps may help you in your quest for inserting items in a listview....


In reality, a ListView is a way to sort data into columns/rows.

The first column of info contains the list view items, and other columns contain subitems (which are associated with each item).

<code>
public MyListView( )
{
//Create our ListView control...
ListView lv = new ListView();
lv.Parent = this;
lv.View = View.Details; //this just specifies how we view data in the listview.

//Create a new 'column' for our listview, named property. The 2nd argument set to -1, to autosize the column.
lv.Columns.Add( "Property", -1, HorizontalAlignment.Left );
lv.Columns.Add( "Value", -1, HorizontalAlignment.Left );

//Create our data to put in our listview.
string [] Properties = { "Property1", "Property2", "Property3" };
string [] Values = { "One", "Two", "Three" };
int iNumProps = Properties.Length; //Get number of props


for( int i = 0; i < iNumProps; i++ )
{
//Create a new listview item, which is the values to go in the 1st column.
ListViewItem lvi = new ListViewItem( Properties );
//Add subitems (things to second column), representing data for the values in the 1st column.
lvi.SubItems.Add( Values );

//Add the listviewitem to our listview
lv.Items.Add( lvi );
}
}
</code>

Now as you see, it's not too difficult, the for loop creates a new listview item each iteration, which makes the columns expand downward with each new value, the subitems make it increase in width... It's not too difficult to add more columns, or more subitems with listview items and subitems for each listview item. I hope this helps

The weevil of doooooooooom
-The eagle may soar, but the weasel never gets sucked up by a jet engine (Anonymous)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top