I have created a form using DataRepeater
and I have an XML file that I deserialize into a class. I then created a dataset
to represent the class. I bind the dataset
to my DataRepeater
. I have also created some labels and textboxes and bind them to the appropriate data from my dataset.
The following are the codes:
TagLabel.DataBindings.Add("Text", ds, "Tag");
PrecedenceLabel.DataBindings.Add("Text", ds, "Precedence");
DataTypeLabel.DataBindings.Add("Text", ds, "DataType");
LengthLabel.DataBindings.Add("Text", ds, "Length");
ValueTextBox.DataBindings.Add("Text", ds, "Value");
MaxTextBox.DataBindings.Add("Text", ds, "Maximum");
MinTextBox.DataBindings.Add("Text", ds, "Minimum");
OverflowTextBox.DataBindings.Add("Text", ds,"OverflowBehaviour");
bindingsource.DataSource = ds;
dataRepeater.DataSource = bindingsource;
dataRepeater.DataMember = "Preference";
After this, I want to hide ValueTextbox
if it's DataTypeLebel
is "Autoinrement"
. The following are the codes:
for (int i = 0; i < dataRepeater.ItemCount; i++)
{
dataRepeater.CurrentItemIndex = i;
if (((Label)dataRepeater.CurrentItem.Controls["DataTypeLabel"]).Text == "AutoIncrement")
{
((TextBox)dataRepeater.CurrentItem.Controls["ValueTextBox"]).Visible = false;
}
}
After I have done this and run the project, in this case according to my XML file, I am expecting only data repeater row 5 has its ValueTextBox
hidden. However, as I scroll the form. I can see some other rows showing invisible VallueTextBox
. It is worse when I scroll up and down and the hidden textboxes are every where. Can anyone suggest what I have done wrong please?
I have also tried virtual mode and update the controls and make textbox invisible based on condition in the event handler dataeRepeater_itemValueNeeded
and the same problem occurs.
If you set a property in a conditional statement such as If…Then, you must also use an Else clause to specify the appearance when the condition is not met. Try this:
if (((Label)dataRepeater.CurrentItem.Controls["DataTypeLabel"]).Text == "AutoIncrement")
{
((TextBox)dataRepeater.CurrentItem.Controls["ValueTextBox"]).Visible = false;
}
else
{
((TextBox)dataRepeater.CurrentItem.Controls["ValueTextBox"]).Visible = true;
}