更改列表视图中的所选项时出错。救命! ! ! ! ! [英] Error while changing the selected item in listview. Help! ! ! ! !

查看:144
本文介绍了更改列表视图中的所选项时出错。救命! ! ! ! !的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为lvSelectCustomer的ListView。

我在lvSelectCustomer中加载了一些值。 lvSelectCustomer有5列。通过将其宽度设置为零来隐藏列[2]和列[4]。因为我不想在运行时显示那些列。所有数据都已成功加载。加载数据没问题。问题是......

我激活lvSelectCustomer事件



private void lvSelectCustomer_ItemSelectionChanged(object sender,ListViewItemSelectionChangedEventArgs e)

{

string strSelectedCustomerIndex = lvSelectCustomer.FocusedItem.Index.ToString();

MessageBox.Show(strSelectedCustomerIndex);

}



在该事件中,我创建了一个名为  strSelectedCustomerIndex的字符串。并将焦点项索引设置为该字符串。当我们在第一次在lvSelectedCustomer中选择一行时,消息框即将到来,并显示所选/焦点行的索引。然后,如果我将从lvSelectedCustomer中选择另一行,则会出现错误。这是...

System.NullReferenceException:'对象引用未设置为对象的实例。'

System.Windows.Forms.ListView.FocusedItem.get返回null。



我需要选择行的索引,而每行选择都会改变。请帮我。我在c#中没那么好。请帮忙整理一下。在此先感谢。



我尝试过:



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:

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

推荐答案

当选择新项目时,事件将触发所选项目更改,旧选定项目将取消选择这会触发事件,因为它是一个更改但是此时没有选择任何内容,因此item为null。



用以下代码包装你的代码:



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 )


这篇关于更改列表视图中的所选项时出错。救命! ! ! ! !的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆