禁用DataRepeater控件中某些项的控件

时间:2009-12-17 04:10:08

标签: c# datarepeater virtualmode

我在C#Winforms应用程序中使用Visual Basic Power Pack中的DataRepeater控件。控件未绑定,在VirtualMode中运行。

我在此控件中显示多个项目。根据特定条件,我想禁用控件中的按钮。

我在数据转发器的_DrawItem事件中尝试了以下内容:

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

根据控件中的最后一项应该启用或禁用按钮会发生什么。

我知道如何逐项控制启用状态吗?

由于

1 个答案:

答案 0 :(得分: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;

希望它有所帮助!

最诚挚的问候!