[ACCEPTED]-C#: How do you make sure that a row or item is selected in ListView before performing an action?-winforms

Accepted answer
Score: 21

I'm not entirely sure what you are asking. Do 5 you want to make sure at least 1 item is 4 selected before running an action? If so 3 the following should work

if ( listView.SelectedItems.Count > 0 ) { 
  // Do something
}

Or are you curious 2 if a particular item is selected? If so 1 try the following

if ( listView.SelectedItems.Contains(someItem)) { 
  // Do something
}
Score: 0
if( listView.SelectedItems.Count > 0 ){
 // do stuff here
}

0

Score: 0

You can also check the value of a selected 1 item or perhaps bind it to a string if needed:

        //Below is with string
        String member = (String)ListView1.SelectedValue;

        //Below is with any class
        AnyClass member = (AnyClass)ListView1.SelectedValue;
        String StaffID = member.StaffID;
Score: 0

int taskId = Convert.ToInt32(itemRow.SubItems[0].Text);

string 2 taskDate = itemRow.SubItems1.ToString();

string 1 taskDescription = itemRow.SubItems[2].ToString();enter image description here

More Related questions