我已经在列表视图中对此进行了管理,以便在列表视图中对项目进行分组,我的客户表格中包含
列 category id
category name
categories
-----------
category name 1
category name 2
category name 3
price ranges
-----------
ALL
0-500
500-1000
我已完成上述任务,但我在列表视图中检查组中的所选项目时遇到问题。
我的问题是如何点击事件,就像我在列表视图中选择第一个组中的第一个项目我想做某事....
如果我在列表视图中选择第二组中的第一项,我想做点什么......
以及我必须在事件中使用所选项目文本的一些内容.....
我如何找到支票......
任何人都可以帮忙......
非常感谢....
这是我的代码
private void categorieslist()
{
lstviewcategories.View = View.Details;
lstviewcategories.Columns.Add(new ColumnHeader() { Width = lstviewcategories.Width - 20 });
lstviewcategories.HeaderStyle = ColumnHeaderStyle.None;
lstviewcategories.Sorting = SortOrder.Ascending;
lstviewcategories.Dock = DockStyle.None;
ListViewGroup categorygroup = new ListViewGroup("Category Types",HorizontalAlignment.Center);
lstviewcategories.Groups.Add(categorygroup);
var categorytypes = (from categories in abc.categories
select categories.category_Name).ToList();
lstviewcategories.Items.Add(new ListViewItem() { Text = "ALL", Group = categorygroup });
foreach (string item in categorytypes)
{
lstviewcategories.Items.Add(new ListViewItem() { Text = item.ToString(), Group = categorygroup });
}
ListViewGroup pricerangegroup = new ListViewGroup("Price Ranges", HorizontalAlignment.Center);
lstviewcategories.Groups.Add(pricerangegroup);
lstviewcategories.Items.Add(new ListViewItem() { Text = "ALL", Group = pricerangegroup });
lstviewcategories.Items.Add(new ListViewItem() { Text = "0-500", Group = pricerangegroup });
lstviewcategories.Items.Add(new ListViewItem() { Text = "500-1000", Group = pricerangegroup });
lstviewcategories.Items.Add(new ListViewItem() { Text = "1000+", Group = pricerangegroup });
}
编辑:
private void lstviewcategories_SelectedIndexChanged(object sender, EventArgs e)
{
// int index = 0;
if (lstviewcategories.SelectedItems.Count > 0 &&lstviewcategories.SelectedItems[0].Group.Name == "Category Types")
{
string text = lstviewcategories.SelectedItems[0].Text.ToString();
var categorywithids = (from categorytypes in abc.categories
where categorytypes.category_Name.Equals(text)
select categorytypes.category_Id).SingleOrDefault();
var productcategoty = from productswithcatgories in abc.product1
where productswithcatgories.category_Id.Equals(categorywithids)
select new
{
productid = productswithcatgories.product_Id, //0
productnam = productswithcatgories.product_Name, //1
productimage = productswithcatgories.product_Image, //2
productprice = productswithcatgories.product_Price,//3
productdescr = productswithcatgories.product_Description,//4
};
productbindingsource.DataSource = productcategoty;
productgridview.DataSource = productbindingsource;
productgridview.Columns[0].Visible = false;
productgridview.Columns[4].Visible = false;
}
}
答案 0 :(得分:2)
尝试创建一个派生自ListViewItem的类,并添加一个枚举属性,您可以在SelectedIndexChanged事件中查询:
public class CustomListViewItem : ListViewItem
{
public CustomListViewItemType Type { get; set; }
}
public enum CustomListViewItemType
{
Type1 = 0,
Type2 = 1
}
lstviewcategories.Items.Add(new CustomListViewItem() { Text = "ALL", Group = pricerangegroup, Type = CustomListViewItemType.Type2 });
void lstviewcategories_SelectedIndexChanged(object sender, EventArgs e)
{
if (lstviewcategories.SelectedItems.Count > 0)
{
CustomListViewItem customListItem = (CustomListViewItem)lstviewcategories.SelectedItems[0];
switch (customListItem.Type)
{
case CustomListViewItemType.Type1:
{
//...
}break;
case CustomListViewItemType.Type2:
{
//...
}break;
}
}
}
答案 1 :(得分:0)
您可以在SelectedIndexChanged
事件中获取SelectedItems,并检查以下组:
private void lstviewcategories_SelectedIndexChanged(object sender, EventArgs e)
{
if(lstviewcategories.SelectedItems.Count > 0 && lstviewcategories.SelectedItems[0].Group.Name == "group name")
//do smth
}
如果启用了MultiSelect属性,例如,您想要在某种按钮上单击选中所选项目,则循环浏览所有选定的项目:
private void button_Click(object sender, EventArgs e)
{
foreach (ListViewItem item in lstviewcategories.SelectedItems)
{
if(item.Group.Name == "group name")
//do smth
}
}