Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select a row in listview

Tags:

c#

.net

listview

I am web developer , working on a part of my project developed in WinForms. So my question could be a basic one. Try to bear with it.

I have two list views on my page and a remove button that works for both.

Problems.

  1. I am not able to select a row in both the list view when I run my program, may be some property needed for it?
  2. If I am able to select the row I want to detect which list view item has been selected, so how would I do that?
  3. I have three columns and have bound the data by using the code below.

        listView1.Columns.Add("ID",20);
        listView1.Columns.Add("Name",40);
        listView1.Columns.Add("Mobile",40);
    
    
        foreach (var item in dataList)
        {
            newItem = new ListViewItem();
            newItem.SubItems.Add(item.ID.ToString());
            newItem.SubItems.Add(item.Name);
            newItem.SubItems.Add(item.Mobile.ToString());
            listView1.Items.Add(newItem);   
        }
    

but the ID column is left blank and the data starts to bind in these sense.

ID Name Mobile
   1    abc
   2    xyz

So how do I properly show the data?

  1. Lastly I want to use my ID column to delete the data. So if I give width=0, is this the best way to hide a column?
like image 875
ankur Avatar asked Sep 26 '12 07:09

ankur


People also ask

How do I select an item in ListView?

You can access all items that are selected in a ListView control by using the ListView. SelectedItems property. Items appear selected only when the ListView control has focus. To select items in response to a user action such as a button click, be sure to call the Focus method in addition to setting this property.

What is Full row select?

The FullRowSelect property is typically used when a ListView displays items with many subitems and it is important to be able to see selected items when the item text is not visible due to horizontal scrolling of the control's contents.


1 Answers

  1. See ListView.FullRowSelect property.
  2. See ListView.SelectedItems property. Note, that by default ListView allows multiselection.
  3. Set item text via constructor: newItem = new ListViewItem(item.ID.ToString());, then add rest of subitems (except of item.ID).
  4. If you want to delete the column, just remove it from the columns collection.
like image 102
Dennis Avatar answered Sep 19 '22 10:09

Dennis