Select a row in listview
Asked Answered
C

2

7

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?
Caernarvonshire answered 26/9, 2012 at 7:17 Comment(0)
H
19
  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.
Hauck answered 26/9, 2012 at 7:33 Comment(6)
thanks for your answer. point no 1,3 has been resolved.on point no 2. i need some inputs , this is what I am doing if(listView1.SelectedItems.Count > 0) { foreach (var item in listView1.SelectedItems) { //how to extract the value of hidden column ie.ID //multi select option is set to be false } }Caernarvonshire
@ankur: do not store value, which you don't want to display in view item. If you just want to use item.ID later without displaying it, bind ListViewItem with data item: newItem.Tag = item. Later you can get it with ((YourDataItemType)item.Tag).ID.Hauck
the approach with tags was pretty gud, but i am not able to get the value. also I have gone through lot of examples of deleting the selected row with multiple columns nothing seems to do the trick. the id that I have kept hidden was actually used for same thing. so can you help me on this my code on delete button.foreach (ListViewItem item in listView2.SelectedItems) { var id = ((int)item.Tag).ID;// this line give error listView2.Items.Remove(item); }Caernarvonshire
@ankur: I'm confusing about what do you want to achieve. Do you want to delete selected items? If so, why don't you just remove them from items collection?Hauck
I want to remove the selected row from the list-view as well delete the same row from database. so for achieving the latter one I need the id of the selected row that I have kept in "tag". I have wrote the code of removing the selected row only thing is I want the tag value of selected item ie "ID".Caernarvonshire
@ankur: I see. If you will put in Tag value of item.ID (newItem.Tag = item.ID), then you could get it back such a way: var id = (int)item.Tag; If you will put the whole data item (newItem.Tag = item), then you could get ID this way: ((YourDataItemType)item.Tag).ID, where YourDataItemType is a type of data item object, not a type of any of its properties.Hauck
P
1

Set listView1.Items[index].Selected=true;

Prudie answered 18/8, 2021 at 0:7 Comment(1)
Welcome to SO. Be cautious about answering 8 year old questions which have confirmed answers. If your answer truely adds value (like a recent update to APIs), go for it but otherwise, best to leave them answered as-is.Target

© 2022 - 2024 — McMap. All rights reserved.