Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C#3.0 DataBinding
Hello, I have created a form using Data Repeater and have inserted some labels and textboxes. Firstly, I deserialized XML file into an object and bind it to the data repeater. I have also binded the labels and textboxes with the appropriate datatable columns. Here are my following codes:
These are the labels and textboxes binded to the data I want to display:
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 some ValueTextbox if it's DataTypeLebel is "Autoinrement" with the following codes:

for (int i = 0; i < dataRepeater.ItemCount; i++)
//Update the CurrentItemIndex                
dataRepeater.CurrentItemIndex = i;
 if (((Label)dataRepeater.CurrentItem.Controls["DataTypeLabel"]).Text == "AutoIncrement")                
 ((TextBox)dataRepeater.CurrentItem.Controls["ValueTextBox"]).Visible = false;           
When the form is loaded, the row 5 ValueTextBox is hidden, which is correct. However, as I scroll down the form, some other ValueTextBoxes are also hidden, whhich are not correct. After a while the ValueTextBoxes are hidden on other rows and basically they are not maintained. Can anyone suggest what have I missed?
Posted 4-Oct-12 2:14am
n.podbielski at 4-Oct-12 7:40am
Did you test if this ValueTextBoxes, that are hidden incorectly, are hidden on server or on JS?
YHiew at 4-Oct-12 8:33am
I am not using server or JS. This is a windows form application. I don't have database, just deserialized from XML.
n.podbielski at 4-Oct-12 8:43am
Oh yeah I missed that. But still. Did you try to breakpoint code hiding those controls?
YHiew at 4-Oct-12 8:45am
Yes I did. I did a console print out where the condition is met and the textbox is made invisible. The print out only happened once which is correct. However, as I scroll the form, some other textboxes appear invisible and randomly. Basically the control property is not maintained in the data repeater
n.podbielski at 4-Oct-12 8:58am
I would bind visible property to some property of the model. I think the problem is current item of repeater. Was not it suppose to change when you scroll?
YHiew at 4-Oct-12 9:09am
I don't understand what your previous comment mean.
NO, it is not suppose to change when I scroll the form. I want to hide the ValueTextBox (i.e. setting control.visible = false) if the row's DataTypeLabel = "Autoincrement". In my XML file, there is only one data which has this property. Therefore when I run my project and load the form, there should be only one (in this case row 5) ValueTextBox is hidden. The problem I am seeing is as I scroll the form, other row's ValueTextBox is hidden as well. I think there is some sort of rendering issue or the form is not updating itself with the changes.
n.podbielski at 4-Oct-12 9:40am
I mean try something like this:
ValueTextBox.DataBindings.Add("Visible", ds, "IsAutoIncrement");
where IsAutoIncrement would be property with hiding logic
IsAutoIncrement {get{
return DataType == "AutoIncrement;
I think that in scroll event dataRepeater.CurrentItem is changing and causing that behavior.
YHiew at 8-Oct-12 5:29am
Do you mean add another column into my dataset (ds) the "AutoIncrement" ? Is there a problem rebinding the ValueTextBox?
n.podbielski at 8-Oct-12 13:44pm
Just add inter mediate tier between tour dataset and grid. Then you can create logic for that kind of thinks.
I have always did things that way and this was least error prone. With such a model controls can listen for property change event and any change in code
refresh controls without data rebinding etc.
Let me know if I was helpful.
YHiew at 9-Oct-12 9:22am
I found the solution, the answer is if I have set a property in a conditional statement such as If…Then, I must also use an Else clause to specify the appearance when the condition is not met.
n.podbielski at 9-Oct-12 9:37am
Great. Maybe add solution to your own answer. :)
It looks like I did not helped you much. Maybe next time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 8,438
1 OriginalGriff 6,491
2 Peter Leow 3,567
3 Zoltán Zörgő 3,326
4 Richard MacCutchan 2,397

Advertise | Privacy | Mobile
Web04 | 2.8.150123.1 | Last Updated 4 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100