【问题标题】:how to find the selected item in list view when we are using groups in listview当我们在列表视图中使用组时如何在列表视图中查找所选项目
【发布时间】:2011-08-24 13:47:36
【问题描述】:

我已经在列表视图中管理了这个,以便在列表视图中对项目进行分组我有客户表有列

                                      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; 

        }
  }

【问题讨论】:

  • 有没有人能帮忙解决这个问题...

标签: c# .net winforms listview


【解决方案1】:

尝试创建一个从 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;
        }
    }
}

【讨论】:

  • 您可以根据需要命名枚举常量以匹配您的组。
  • 那么在这种情况下如何使用项目文本 CustomListViewItemType.Type1: { //... }break;
  • 你可以像string text = customListItem.Text;那样访问ListViewItems Text属性
【解决方案2】:

您可以在 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
    }
}

【讨论】:

  • 如果我输入这一行 if(lstviewcategories.SelectedItems[0].Group.Name == "group name") 我得到了像 InvalidArgument=Value of '0' is not valid for 'index 这样的错误'。参数名称:索引
  • 对不起,我忘了检查是否至少选择了一项。编辑答案。已添加lstviewcategories.SelectedItems.Count > 0
  • 但它不工作......我已经更新了我的代码......查看我更新的代码它不会进入 if 循环......
  • @user899271 问题是尚未设置组名称属性(仅 Header 属性)。更改您的支票,它将起作用。但是,像这样检查魔术字符串并不是一个很好的做法,并且可能会导致以后的维护噩梦。
  • @user899271,你可以这样设置:categorygroup.Name = "..";
猜你喜欢
  • 2011-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-12
  • 1970-01-01
相关资源
最近更新 更多