Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
DataBinding a CheckBox
Message
From
15/11/2002 15:43:18
 
General information
Forum:
ASP.NET
Category:
Forms
Miscellaneous
Thread ID:
00715187
Message ID:
00723412
Views:
8
Ken,

Well, even though we've run into a few problems, I would *not* do it any other way ... data binding is the way to go for all WinUI objects, IMHO. You had problems data binding things like TextBoxes? I can't imagine why ....

~~Bonnie


>Good Luck with the databinding. We are finishing a winApp using data binding extensively. I will not do that again. The only time data binding seems to be the correct choice is for combo boxes and DataGrids.
>
>
>Ken
>
>>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