2009-12-17 103 views
1

我在我的C#Winforms應用程序中使用Visual Basic Power Pack中的DataRepeater控件。該控件未綁定,在VirtualMode中運行。禁用DataRepeater控件中某些項目的控件

我在此控件中顯示多個項目。根據某些標準,我想禁用控件中的按鈕。

我試圖在數據轉發的_DrawItem事件如下:

private void dataXYZ_DrawItem(object sender, DataRepeaterItemEventArgs e) 
{ 
    int Item=e.DataRepeaterItem.ItemIndex; 
    dataXYZ.CurrentItem.Controls["buttonSomething"].Enabled = SomeFunc(Item); 
} 

發生什麼按鈕是基於控制的最後一個項目應該是什麼啓用或禁用。

任何想法如何我可以逐項控制啓用狀態?

感謝

回答

3

如果你想你的循環項目的DataRepeater,你可以做這樣的事情:

  //Store your original index 
      int intOldIndex = dataRepeater1.CurrentItemIndex; 

      //Loop through datarepeater items and disabled them 
      for (int i = 0; i < dataRepeater1.ItemCount; i++) 
      { 
       //Just change the CurrentItemIndex and the currentItem property will get the element from datarepeater! 
       dataRepeater1.CurrentItemIndex = i; 
       dataRepeater1.CurrentItem.Enabled = false; 

       //You can access some controls in the current item context 
       ((TextBox)dataRepeater1.CurrentItem.Controls["txtName"]).Text = "My Name"; 

       //If your textbox is inside a groupbox, for example, 
       //you'll need search the control because it is inside another 
       //control and the textbox will not be accessible 
       ((TextBox)dataRepeater1.CurrentItem.Controls.Find("txtName",true).FirstOrDefault()).Text = "My Name"; 
      } 

      //Back your original index 
      dataRepeater1.CurrentItemIndex = intIndex; 
      dataRepeater1.CurrentItem.Enabled = true; 

希望它能幫助!

最好的問候!