我将listView的MultiSelect
属性设置为false,并且我正在尝试获取单个listViewItem。但可用的财产是SelectedItems
。我一直在使用以下代码......
foreach (ListViewItem item in listView1.SelectedItems)
{
//do something with item.text or whatever
}
因为我知道只会选择一个项目。这样做的正确方法是什么?
通常SelectedItems
返回集合,数组或IQueryable
。
无论哪种方式,您都可以通过索引访问项目与数组:
String text = listView1.SelectedItems[0].Text;
//do something
顺便说一句,您可以随时将要查看的项目保存到var lookat = whatever you want
and并在设置断点后检查其在Locals中的结构。
我这样做:
if (listView1.SelectedItems.Count > 0)
{
var item = listView1.SelectedItems[0];
//rest of your logic
}
有时只使用下面的行会抛出异常,
String text = listView1.SelectedItems[0].Text;
所以我使用下面的代码:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedIndices.Count <= 0)
{
return;
}
int intselectedindex = listView1.SelectedIndices[0];
if (intselectedindex >= 0)
{
String text = listView1.Items[intselectedindex].Text;
//do something
//MessageBox.Show(listView1.Items[intselectedindex].Text);
}
}
如果它只是一个带有一个或两个ListViews
的小应用程序,我通常会创建一个小帮手属性:
private ListViewItem SelectedItem { get { return (listView1.SelectedItems.Count > 0 ? listView1.SelectedItems[0] : null); } }
如果我有加载,则将其移出到辅助类:
internal static class ListViewEx
{
internal static ListViewItem GetSelectedItem(this ListView listView1)
{
return (listView1.SelectedItems.Count > 0 ? listView1.SelectedItems[0] : null);
}
}
所以:
ListViewItem item = lstFixtures.GetSelectedItem();
ListView
界面有点垃圾,所以我通常发现助手类增长很快。
对于购物车情况,这是我推荐的。我要把它分解成最简单的形式。
假设我们从这开始(带有2列,2个按钮和标签的列表视图):
首先,删除项目,为此我们将输入我们的删除按钮:
private void button2_Click(object sender, EventArgs e)
{
listView1.Items.Remove(listView1.SelectedItems[0]);
label1.Text = updateCartTotal().ToString();
}
现在第二行是使用我将发布的下一个函数更新我们的标签总数,以便在列表视图中添加第2列的所有内容:
private decimal updateCartTotal()
{
decimal runningTotal = 0;
foreach(ListViewItem l in listView1.Items)
{
runningTotal += Convert.ToDecimal(l.SubItems[1].Text);
}
return runningTotal;
}
你不必像我一样使用十进制,如果你没有小数,你可以使用float或int。所以让我们分解吧。我们使用for循环来计算第2列中的所有项(SubItems [1] .Text)。将它添加到我们在foreach循环之前声明的小数以保持总计。如果你想做税,你可以这样做:
return runningTotal * 1.15;
或者你的税率是多少。
它的长短,使用此功能,您只需调用该函数即可重新调整列表视图。您可以更改标签文本,就像我之前演示的那样,如果这就是您所追求的。
上面的答案,至少对我而言,都没有显示如何实际处理确定您是否有1个项目或多个项目,以及如何以通用方式实际获取项目中的值,而不依赖于实际上只有一个项目,或多个,所以我把我的帽子扔在戒指。
通过检查您的计数以确定您至少有一个项目,然后在foreach
上执行.SelectedItems
循环,将每个项目作为DataRowView
投射,这非常容易且通常完成:
if (listView1.SelectedItems.Count > 0)
{
foreach (DataRowView drv in listView1.SelectedItems)
{
string firstColumn = drv.Row[0] != null ? drv.Row[0].ToString() : String.Empty;
string secondColumn = drv.Row[1] != null ? drv.Row[1].ToString() : String.Empty;
// ... do something with these values before they are replaced
// by the next run of the loop that will get the next row
}
}
这将有效,无论您有1项还是多项。有趣的是,MSDN says使用ListView.SelectedListViewItemCollection
捕获listView1.SelectedItems
并迭代,但我发现这在我的WPF应用程序中出错了:The type name 'SelectedListViewItemCollection' does not exist in type 'ListView'
。
这适用于单个和多个选择列表:
foreach (ListViewItem item in listView1.SelectedItems)
{
int index = ListViewItem.Index;
//index is now zero based index of selected item
}