Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
DataBinding a CheckBox
Message
General information
Forum:
ASP.NET
Category:
Forms
Miscellaneous
Thread ID:
00715187
Message ID:
00717903
Views:
12
Doug,

Didn't you sub-class all the controls in VFP? You should do it in .NET for the same reason ... whether you think you're gonna need to expand on the base controls or not, you should just do it ... because you never know. One thing that we've done in all our sub-classes is a DataBind() method ... simply because it's easier to have a way of controlling this by having a "default" binding for the various controls. This way we can also have Unbind() and Rebind() methods ... something we've definitely found a use for with some controls such as comboboxes.

Anyway, I think subclassing all these controls is just one of those things that I consider "best practice" in any programming language.

Just my 2 cents ...

~~Bonnie


>Bonnie,
>
>I have yet to subclass all of the .NET form controls - besides the CheckBox, I am not yet seeing what subclassing say, TextBox, would do for me? I know I will in the long run, but could you give me an example? Are your changes basically stylistic?
>TIA,
>Doug
>>If anyone cared about this question I posted awhile ago, I just wanted to let you all know that I found a pretty acceptable workaround for this bug.
>>
>>You need to have a sub-classed checkbox (but we *all* use sub-classed objects for our base classes now don't we? If you don't, you should <g>)
>>
>>Here's the way I do it:
>>
>>You need a binding field, a DataBind() method and a Format EventHandler. Like this:
>>
>>
>>public class MyCheckBox : System.Windows.Forms.Checkbox
>>{
>>  protected Binding oBinding = null
>>
>>  public virtual void DataBind(System.Data.DataTable Data, string Field)
>>  {
>>    this.oBinding = new Binding("Checked", Data, Field);
>>    this.oBinding.Format += new ConvertEventHandler(this.FormatHandler);
>>    this.DataBindings.Add(this.oBinding);
>>  }
>>
>>  private void FormatHandler(object sender, ConvertEventArgs e)
>>  {
>>    if (e.Value == DBNull.Value)
>>      e.Value = false;
>>  }
>>}
>>
>>
>>That's it!! After all that banging my head against the wall, the solution is quite simple. Now if it's a NULL, the BindingContext.Position doesn't choke on it anymore, because it get's changed to a false.
>>
>>Anyway, I wanted to share this solution with everyone to save others from banging *their* heads against the proverbial wall ... <g>
>>
>>~~Bonnie
>>
>>
>>
>>>Hi all,
>>>
>>>Sorry I haven't been answering many questions here lately ... I've been head-down in coding 24/7. <g> But I have a question that hopefully someone else will be able to answer.
>>>
>>>I have a control with a checkbox on it that I would like to bind to a boolean column in a DataSet. The DataSet is filled from a SQL Server table and it *does* allow NULLs in the column.
>>>
>>>I can't bind to the CheckState (blows up with an error about "Invalid cast from System.Boolean to System.Windows.Forms.CheckState"), so I'm binding to Checked (which seems right to me anyway). I've set the CheckBox.ThreeState = true (since the column *can* have a NULL value).
>>>
>>>I seem to be getting a problem though when I attempt to change the BindingContext.Position to a row that has a DBNull.Value in this column. I get no error that I can see, but the BindingContext.Postion just doesn't change ... it remains on whatever row it was already on rather than go to the row with the null value.
>>>
>>>What's up with that? Anyone know of a work-around or am I just doing things wrong?? (BTW, this behavior happens whether I have ThreeState either true or false).
>>>
>>>TIA,
>>>~~Bonnie
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Reply
Map
View

Click here to load this message in the networking platform