开发者

how to find the selected item in list view when we are using groups in listview

I have managed this in list view for grouping items in list view i have customer table have columns with

                                      category id
                                     category name 




                       categories
                    -----------
                    category name 1
                    category name 2
                    category name 3

                    price ranges
                    -----------
                    ALL
                   0-500
                   500-1000

I have done above task but i have problem with checking selected item in groups in list view ..

how to find the selected item in list view when we are using groups in listview

my problem is how we fire the event like if i select the first item in first group in list view i want to do something ....

and if i select the first item in second group in list view i want do something...

and some where i have to use the selected item text in events.....

how i find the checking ...

can any one help on this .....

Many thanks....

and this is my code

    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 Lis开发者_Go百科tViewItem() { 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 });   
    }

EDIT :

        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; 

        }
  }


Try creating a class derived from ListViewItem and add an enumeration property which you can query in the SelectedIndexChanged event:

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;
        }
    }
}


You can get SelectedItems for example in SelectedIndexChanged event and check group like below:

private void lstviewcategories_SelectedIndexChanged(object sender, EventArgs e)
{
    if(lstviewcategories.SelectedItems.Count > 0 && lstviewcategories.SelectedItems[0].Group.Name == "group name")
       //do smth    
}

if MultiSelect property is enabled and for example you want to check selected items on some kind button click, loop through all selected items:

private void button_Click(object sender, EventArgs e)
{
    foreach (ListViewItem item in lstviewcategories.SelectedItems)
    {
       if(item.Group.Name == "group name")
           //do smth
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜