2009-12-17 111 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; 

希望它能帮助!

最好的问候!