C# – Disable controls on certain items in DataRepeater control

cdatarepeatervirtualmode

I'm using the DataRepeater control from the Visual Basic Power Pack in my C# Winforms application. The control is unbound, operating in VirtualMode.

I'm displaying multiple items in this control. Depending on certain criteria, I want to disable a button in the control.

I've tried the following in the _DrawItem event of the data repeater:

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

What happens is the button is enabled or disabled based on what the last item in the control should be.

Any idea how I can control enable state on an item by item basis?

Thanks

Best Answer

If you want to loop your datarepeater items, you can do something like this:

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

Hope it helps!

Best Regards!

Related Topic