2011-08-24 40 views
0

如何找到在列表視圖中選擇的項目我已經在列表視圖中管理這個在列表視圖分組項目,我有客戶表有列與當我們使用組列表視圖

         category id 
            category name 




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

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

我在上面所做的的任務,但是我有檢查列表視圖組選定項目的問題.. enter image description here

我的問題是,我們怎麼火成那樣,如果我選擇在列表視圖中第一組的第一個項目我想要做的事情況.. ..

如果我選擇在我想要做一些事情列表視圖中第二組的第一項...

,有些地方我必須使用事件中所選擇的項目文本.....

我如何找到檢查...

可以在此任意一個幫助.....

非常感謝....

,這是我的代碼

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

將在此任何一個請幫助... –

回答

0

你可以得到例如SelectedItems在SelectedIndexChanged事件,並檢查組象下面這樣:

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

如果啓用了多選屬性和例如要檢查所選項目點擊某種按鈕,循環選擇所有項目:

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

如果我把這行如果(lstviewcategories.SelectedItems [0] .Group.Name ==「組名稱」)我得到像InvalidArgument ='0'的值的錯誤是無效'索引'。 參數名稱:索引 –

+0

你能否解決這個問題 –

+0

對不起,我忘了檢查是否至少選擇了一個項目。編輯答案。添加'lstviewcategories.SelectedItems.Count> 0' – Reniuz

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

我可以認爲類型1和類型2是兩個組.. –

+0

您可以根據您的需要將枚舉常量命名爲匹配您的組。 – jdavies

+0

所以我如何使用這個案例中的項目文本案例CustomListViewItemType.Type1: { // ... } break; –

相關問題