[1.0/1.1] Adding items to Columns
Hi all,
I've created two columns on the list view as follows.
Code:
ColumnHeader colHed;
// Header one
colHed = new ColumnHeader();
colHed.Text = "Name";
colHed.Width = 100;
this.lwDetails.Columns.Add(colHed);
// Header two
colHed = new ColumnHeader();
colHed.Text = "Address";
colHed.Width = 200;
this.lwDetails.Columns.Add(colHed);
Then on a separate function I try to add data to those columns. Name and address of a one person store in a string array named items. I try to add data as follows.
Code:
this.lwDetails.Items.Add(items[0]);
this.lwDetails.Items.Add(items[1]);
lwDetails is my list view. In this case name and address are added to the same column. How can I correct it.
Thanks,
Re: [1.0/1.1] Adding items to Columns
Each item occupies a row in the ListView. If you want to fill more columns on a row then you have to add subitems to that item.
Re: [1.0/1.1] Adding items to Columns
Use a ListViewItem object to add your SubItems.
C# Code:
private void Form1_Load(object sender, EventArgs e)
{
ColumnHeader colHed;
// Header one
colHed = new ColumnHeader();
colHed.Text = "Name";
colHed.Width = 100;
this.lwDetails.Columns.Add(colHed);
// Header two
colHed = new ColumnHeader();
colHed.Text = "Address";
colHed.Width = 200;
this.lwDetails.Columns.Add(colHed);
// Setup Listivew stuff
this.lwDetails.View = View.Details;
this.lwDetails.FullRowSelect = true;
this.lwDetails.HideSelection = false;
this.lwDetails.MultiSelect = false;
// load some data
ListViewItem lvwItem;
lvwItem =this.lwDetails.Items.Add(new ListViewItem("Test1"));
lvwItem.SubItems.Add("SubItem1");
lvwItem = this.lwDetails.Items.Add("Test2");
lvwItem.SubItems.Add("SubItem2");
}
Re: [1.0/1.1] Adding items to Columns
Code:
// load some data
ListViewItem lvwItem;
lvwItem =this.lwDetails.Items.Add(new ListViewItem("Test1"));
lvwItem.SubItems.Add("SubItem1");
lvwItem = this.lwDetails.Items.Add("Test2");
lvwItem.SubItems.Add("SubItem2");
I'm not clear what happened on this code, I mean how to find the "Name" column to add the relevant data to it. And "Address" column also. Can you explain it little more.
1 Attachment(s)
Re: [1.0/1.1] Adding items to Columns
"SubItem1" and "SubItem2" are the text of the subitems being added.
By setting the lvwItem object to the resulting .Add of the new listviewitem we get a object reference set to that exact item/row. Using the object variable we can access the properties and methods of the row. .SubItems.Add is the method for adding new subitems/text to the other secondary columns. ;)
Re: [1.0/1.1] Adding items to Columns
Oki, I got the point. And also I no need to set the Details property of the ListView, since I can set it on the Properties dialog box. http://www.vbforums.com/showthread.p...96#post3108596
Re: [1.0/1.1] Adding items to Columns
Yes that is true but I was doing it for a demo and as its my preference to code in the setup of the controls instead of property wwindow property setting changes. Explicit code setup is just a matter of personal preference.
That link goes back to your original post?
Re: [1.0/1.1] Adding items to Columns
Thanks Rob, I got the point.
And sorry, that link put by mistake.