I have created a listView on my Form. In the properties I have created 2 Columns.
What I now wonder is how you can put text in each column.
I have tried out this code but no Text is coming up in the listView1 at all.
Could I be on the right track ?

listView1->Columns[0]->Text = "Column1";
listView1->Columns[1]->Text = "Column2";

Unfortunately, it's not that simple.

First, be sure you change the "View" property of the ListView to "Details" so you will see your columns.

Next, you need to add a ListViewItem to a ListView, like this:

ListViewItem ^li = gcnew("Col 1 Value");
li->SubItems->Add("Col 2 Value");
listView1->Items->Add(li);

You can keep calling li->SubItems->Add("...") for as many columns you need.

Thank you very much. It worked well.

When I have selected a Row in the listView1 that has 2 columns, I want to display this String that is in Column1 on that selected row.
If I put this code, I will have this message:

System.Window.Forms.ListView+SelectedListViewItemCollection

At the same time I think I have to specify what column I want the text from but cant find any members to listView2->Column-> that I can think of works:

String^ GetName = listView2->SelectedItems->ToString();
MessageBox::Show(GetName);

After some experiments, I came up with this:

for( int i = 0; i < listView2->Items->Count; i++ )
{
     if( listView2->Items[i]->Selected)
    {
	GetName= listView2->Items[i]->Text;
    	MessageBox::Show(GetName);
							
    }
}

There is one thing I have a problem with or wonder if it is possible...
I use 2 controls of listView. listView1 and listView2 on same Form.

In these listViews I have 5 items each. If I click one item in listView1 this row will select.
If I now click on an item in listView2 this row will select and the row in listView1 will ofcourse Deselect.

So my question will be, is it possible to select an item in both.

I solved this with another approach where I put selected items in labels and read them from there.

There is one thing I have a problem with or wonder if it is possible...
I use 2 controls of listView. listView1 and listView2 on same Form.

In these listViews I have 5 items each. If I click one item in listView1 this row will select.
If I now click on an item in listView2 this row will select and the row in listView1 will ofcourse Deselect.

So my question will be, is it possible to select an item in both.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.