sark9012
sark9012

Reputation: 5747

Selectedindex in listview, throwing up an error!

Hey guys, the following code shows what i am trying to do.

private void btnEdit_Click(object sender, EventArgs e)
    {
        iDeliverySelected = lstDeliveryDetails.SelectedIndex;
        bool addEdit = false;
    }

The selectedindex is throwing up the following error.. 'System.Windows.Forms.ListView' does not contain a definition for 'SelectedIndex' and no extension method 'SelectedIndex' accepting a first argument of type 'System.Windows.Forms.ListView' could be found (are you missing a using directive or an assembly reference?)

Any ideas why? First time I have tried to use SelectedIndex, not sure if i am using it correctly?

Upvotes: 0

Views: 2992

Answers (2)

Hans Olsson
Hans Olsson

Reputation: 55009

There can be more than one selection so look at:

ListView.SelectedIndices

Upvotes: 0

Oded
Oded

Reputation: 499062

It is because the ListView class does not have a SelectedIndex property. It has a SelectedIndices property.

ListView.SelectedIndexCollection indexes = this.ListView1.SelectedIndices;

double price = 0.0;
foreach ( int index in indexes )
{
  price += Double.Parse(this.ListView1.Items[index].SubItems[1].Text);
}

Upvotes: 1

Related Questions