C# 获取列表视图子项的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15541901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get the value of a listview subitem
提问by Harold
I have a code in C#. I have a listview then what I want is to get the value of the subitem specifically the Subtotal subitem which contains the subtotal price of the orders. I want to sum up the values and display to result to a textbox in the same form. How can I do that? Here's my code:
我有一个 C# 代码。我有一个列表视图,然后我想要的是获取子项的值,特别是包含订单小计价格的小计子项。我想总结这些值并显示为相同形式的文本框。我怎样才能做到这一点?这是我的代码:
AddOrderForm add = new AddOrderForm();
foreach (DataRow drow in dt.Rows)
{
add.nametb.Text = drow["Menu"].ToString();
add.Pricetb.Text = drow["MenuPrice"].ToString();
add.listBox1.Items.Add(drow["MenuDesc"].ToString());
add.orderqtytb.Focus();
add.ShowDialog();
}
if (add.isAddOrder)
{
ListViewItem item = new ListViewItem();
item.Text = add.nametb.Text;
item.SubItems.Add(add.orderqtytb.Text);
item.SubItems.Add(add.Pricetb.Text);
double total = 0;
try
{
total = double.Parse(add.Pricetb.Text) * double.Parse(add.orderqtytb.Text);
}
catch
{
total = 0;
}
item.SubItems.Add(total.ToString("0.00"));
Listview.Items.Add(item);
采纳答案by Manish Mishra
you can retrieve an Item
value or its SubItems
in a ListView
this way:
您可以通过以下方式检索Item
值或其值:SubItems
ListView
suppose your ListView is like this:
假设你的 ListView 是这样的:
public void AddDataToLvw(){
ListViewItem item1 = new ListViewItem("item1", 0);
item1.SubItems.Add("1");
item1.SubItems.Add("3");
ListViewItem item2 = new ListViewItem("item2", 1);
item2.SubItems.Add("4");
item2.SubItems.Add("6");
ListViewItem item3 = new ListViewItem("item3", 0);
item3.SubItems.Add("7");
item3.SubItems.Add("9");
// Create columns for the items and subitems.
listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left);
listView1.Items.AddRange(new ListViewItem[] { item1, item2, item3 });
}
then you can access its subitems like this:
然后你可以像这样访问它的子项:
private void GetSubItems()
{
int total = 0;
foreach (ListViewItem item in listView1.Items)
{
total += Convert.ToInt32(item.SubItems[1].Text);
}
MessageBox.Show(total.ToString());
}
notice the index that I've passed in item.SubItems
. you should pass that subitem index, which you want to sumup
注意我传入的索引item.SubItems
。您应该传递要总结的子项索引
回答by masterchris_99
I would prefer to construct a class like this
我更喜欢构造这样的类
class MyClass
{
public string PropertyA { get; set; }
public string PropertyB { get; set; }
public string PropertyC { get; set; }
public string PropertyD { get; set; }
}
While building the Listview you generate this class fill the properties and add the object of the class to the Tag of the ListViewItem
在构建 Listview 时,您生成此类填充属性并将类的对象添加到 ListViewItem 的 Tag
MyClass myClass = new MyClass();
myClass.PropertyA = "A";
myClass.PropertyB = "B";
myClass.PropertyC = "C";
myClass.PropertyD = "D";
ListViewItem item = new ListViewItem();
item.Text = myClass.PropertyA;
item.SubItems.Add(myClass.PropertyB);
item.SubItems.Add(myClass.PropertyC);
item.Tag = myClass;
Now you can take the object from the Tag and you have your values
现在您可以从 Tag 中取出对象并获得您的值
MyClass myClassForLaterUse = (MyClass)item.Tag;
MessageBox.Show(myClassForLaterUse.PropertyA);
回答by Arun
Your code already adds the items to the Listview correctly, Assuming you want to retrieve the value of column 'total' on say SelectedIndexChanged event, you can do -
您的代码已经正确地将项目添加到 Listview,假设您想在 SelectedIndexChanged 事件中检索列 'total' 的值,您可以这样做 -
ListView.Items[SelectedItemIndex].SubItem[2].Text