Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a ListView named lvSelectCustomer.
I loaded some values in lvSelectCustomer. The lvSelectCustomer has 5 columns. Column[2] and Column[4] are hidden by set its Width into zero. Because i don't want to show those colums while runtime. All data are loaded successfully. No problem in loading data. The problem is...
I activate an event of lvSelectCustomer

private void lvSelectCustomer_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
string strSelectedCustomerIndex = lvSelectCustomer.FocusedItem.Index.ToString();
MessageBox.Show(strSelectedCustomerIndex);
}

Inside that event i created a string called strSelectedCustomerIndex . And set the focused item index to that string. While we select a row in lvSelectedCustomer at the 1st time the message box is coming and shows the index of that selected/focused row. Then after that, if i will select an another row from lvSelectedCustomer, an error is coming. Which is...
System.NullReferenceException: 'Object reference not set to an instance of an object.'
System.Windows.Forms.ListView.FocusedItem.get returned null.

I need the selected row's index while every row selection change. Please help me. I am not that much good in c#. Please help to sort it out. Thanks in advance.

What I have tried:

C#
private void lvSelectCustomer_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            string strSelectedCustomerIndex = lvSelectCustomer.FocusedItem.Index.ToString();
            MessageBox.Show(strSelectedCustomerIndex);
        }
Posted
Updated 23-Apr-17 8:29am

1 solution

The event fires for selected item change, when a new item is selected the old selected item deselects first and this fires the event as it is a change however nothing is selected at that point so item is null.

wrap your code with:

if (lvSelectCustomer.SelectedItems.Count != 0 )
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900