Bind MaxLength to control

Posts   
 
    
Sokon1
User
Posts: 97
Joined: 17-Jul-2006
# Posted on: 07-Aug-2006 16:10:00   

Hi, I'd like to bind the MaxLength-Property of a TextBox to the MaxLength-Value of the original SQL-Server-Column. Is that possible?

I tried somthing like: this.TextBox1.DataBindings.Add(new System.Windows.Forms.Binding("MaxLength", this.bsContent, "???"));

But I don't know what to write instead of the "???" bsContent is a Bindingsource which contains a entity collection.

Thanks in advance!

Jessynoo avatar
Jessynoo
Support Team
Posts: 296
Joined: 19-Aug-2004
# Posted on: 07-Aug-2006 17:03:54   

Hi

You'll be able to access the maxlenght through something like:

yourEntity.Fields("yourFieldname").MaxLength

Then it cannot be as straight forward as you wish to do it, but it shouldn't be that difficult with a little code of yours.

Sokon1
User
Posts: 97
Joined: 17-Jul-2006
# Posted on: 08-Aug-2006 08:26:49   

Hey, thanks for your answer! "yourEntity" is an instance, isn't it?

using this:

this.TextBox1.DataBindings.Add(new Binding("Text", bsContent, "TxtNote", true)); The DataSource parameter is a constant.

if i use something like this.TextBox1.DataBindings.Add(new Binding("Maxlength", bsContent, "constant??", true)); i should be able to use a constant value too?

Because: if i used a value coming from the "myEntity" instance, the code would fail (null exception), if there is no instance. This is the case when there is an empty collection i want to add a new entity with bound controls. Then there is no initial entity,whose maxlength property i could use.

Is there no other way to get maxlength? In my understanding maxlength should be a static property of the entitytype, or do I miss something?

Sokon1
User
Posts: 97
Joined: 17-Jul-2006
# Posted on: 08-Aug-2006 08:57:40   

Uh-oh. my fault. Sorry for your time. simple_smile I didn't really want to bind the maxlength value. It doesn't make sense to bind a value to a control which shows different data, but always the same type of data. I just wanted to set the value once in the code so it will fit even if the data schema is changed and LLBLGen created new code.

This will do it, i think: TextBox1.MaxLength = ((PartnerEntity)new PartnerEntity()).Fields["StrDescription"].MaxLength;

Not handy, but it works! I still think a static property would be more logical ;-)

Jessynoo avatar
Jessynoo
Support Team
Posts: 296
Joined: 19-Aug-2004
# Posted on: 08-Aug-2006 11:24:13   

Actually "PartnerFields.StrDescription" should give you access to the field outside of an instance. So

PartnerFields.StrDescription.MaxLength probably is what you want to do.

Sokon1
User
Posts: 97
Joined: 17-Jul-2006
# Posted on: 21-Aug-2006 16:48:39   

Ah yes! That's it! Thank you very much. Shame on me, i searched for hours and didn't find it flushed