Search code examples
c#winformscheckboxdatarepeater

How to retrieve the check state of CheckBox from database while the CheckBox control is on the DataRepeater


I want to retrieve the checked state of the unbound CheckBox control on the DataRepeater and database using C# in win forms. I dragged the "Task State" field from Task table as a label containing the 0 (which will be used as false) and 1 (which will be used as true for checkBox1.Checked).

Screenshot

as you can see in the image, I want the unbound CheckBox to take it's check state from translating those 1s and 0s from the label under the CheckBox (green arrowed).

This is the code I'm using to set the checkboxes:

private void dataRepeater3_DrawItem(object sender, Microsoft.VisualBasic.PowerPacks.DataRepeaterItemEventArgs e) 
{
    if (((Label)dataRepeater3.CurrentItem.Controls["stateLabel1"]).Text == "1") 
    { 
        ((CheckBox)dataRepeater3.CurrentItem.Controls["checkBox1"]).Checked = true; 
    } 
    else 
    { 
        ((CheckBox)dataRepeater3.CurrentItem.Controls["checkBox1"]).Checked = false; 
    } 
}

Solution

  • You need to loop through the controls each time you draw the repeater. Try this in your DrawItem method:

    foreach ( DataRepeaterItem rowItem in dataRepeater1.Controls )
        {
            if (((Label)rowItem.Controls["stateLabel1"]).Text == "1") 
            { 
                ((CheckBox)rowItem.Controls["checkBox1"]).Checked = true; 
            } 
            else 
            { 
                ((CheckBox)rowItem.Controls["checkBox1"]).Checked = false; 
            } 
        }