Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Binding Checkbox to nchar column
Message
 
 
To
31/01/2010 14:04:54
General information
Forum:
ASP.NET
Category:
Forms
Environment versions
Environment:
C# 2.0
Miscellaneous
Thread ID:
01445996
Message ID:
01446746
Views:
18
Bonnie,

In your method DataBind what is the m_BoundProperty? And do I need to create a property "BBCheckBox_CheckedChanged"?

>Hi Dmitry,
>
>>What confuses me is why do I need to create a method in BBCheckBox instead of using the stock (the method of the .NET checkbox class)?
>
>If you go to the trouble of creating sub-classes and creating a DataBind() method for all those sub-classes, then everything in the sub-class "feeds" off of that fact. You *have* to use *your* DataBind() method for everything and *not* use the .NET DataBindings.Add() method at all.
>
>Now, what I had suggested was that you create an overloaded DataBind() method to the BBCheckBox class in order to bind to a BindingSource. Don't replace the existing DataBind(), which binds to the DataTable, simply add the overload, which binds to the BindingSource. Until you do that, you'll still have problems.
>
>I'm not 100% sure the following change will work in all cases (I *did* try it out with the issue you're having and it seems fine for that).
>
>
>public virtual void DataBind(BindingSource bs, string Path)
>{
>	this.DataBindings.Clear();
>
>	this.Checked = false;
>	this.m_BoundTable = (DataTable)bs.DataSource;
>	this.m_BoundColumn = Path;
>
>	this.oBinding = new Binding(this.m_BoundProperty, bs, Path);
>	this.oBinding.Format += new ConvertEventHandler(this.FormatHandler);
>	this.oBinding.Parse += new ConvertEventHandler(this.ParseHandler);
>	this.DataBindings.Add(this.oBinding);
>
>	this.CheckedChanged += new EventHandler(BBCheckBox_CheckedChanged);
>
>	this.oCurrency = (CurrencyManager)this.BindingContext[bs];
>}
>
>
>
>
>>Sorry Bonnie but this issue with a check box is driving me crazy. Absolutely! Can you think of why the following does not work?
>>
>>When I bind the check box (based on your class BBCheckBox) to the BindingSource using the following syntax:
>>
>>
>>this.chkStatus.DataBindings.Add("Checked", bindingSource1, "VendorStatus");
>>
>>
>>everything works.. except when adding a new row to the grid and the datatabe of the dataset. There is something about check box stock method DataBinding.Add (because I understand that this is what being used since I didn't override this method with my own) that causes the issue when the grid points to the new record (marked with *).
>>
>>Or, if above is too confusing to answer, the following question. If I change your method DataBind of BBCheckBox to receive BindingSource as a parameter instead of the DataTable, wouldn't it create a problem since the method DataBind already has call to .DataBinding.Add()?
>>
>>What confuses me is why do I need to create a method in BBCheckBox instead of using the stock (the method of the .NET checkbox class)?
>>
>>
>>>Hi Dmitry,
>>>
>>>As shown below, my BBCheckBox class binds to a DataTable. However, if you're using BindingSource for your binding rather than DataTables, you'll have other issues. Remember, the way you bind must be consistent for every control on your Form. If you're going to bind to a BindingSource, then *all* controls on your Form must bind to the same BindingSource or they will not stay in sync (likewise, if you bind to a DataTable, all controls on your form must bind to the same DataTable ... well, those controls that are supposed to stay in sync with the same data, that is).
>>>
>>>Consequently, you may want to add another DataBind method to that class to accept a BindingSource rather than a DataTable.
>>>
>>>However, I don't think that's your problem in this case (haven't tested it, just guessing) ... I think it may be more related to the need to commit the proposed changes (remember prior posts here about that).
>>>
>>>~~Bonnie
>>>
>>>
>>>
>>>>Hi Bonnie,
>>>>
>>>>I was wondering if you could make any suggestions on why my data set table column bound to the check box does not get updated after I click on the check box. The check box visibly changes to unchecked (or if it was unchecked to checked). But when I in code test the value in the table column does not change.
>>>>
>>>>I am using your checkbox subclass and bound it to the column of Boolean type (the type in SQL Server is BIT).
>>>>
>>>>Interesting that if I use a standard checkbox class and bind it using syntax
>>>>
>>>>
>>>>this.chkStatus.DataBindings.Add("Checked", bindingSource1, "VendorStatus");
>>>>
>>>>
>>>>then the value in the table column changes. But I have other issues with the standard checkbox class. So I prefer to use your version of the class code (as shown below). And I bind the column then as you suggested:
>>>>
>>>>
>>>>this.MyCheckBox.DataBind(MyTable, "MyYNColumn");
>>>>
>>>>
>>>>But, as I said, this does not update the data set/data table/column. Please let me know if you have any suggestions.
>>>>
>>>>>Hi Dmitry,
>>>>>
>>>>>Do you have a checkbox subclass? You'll need one. In fact, I have two checkboxes, one for binding to normal bool values and one for binding to string values. Your code might have to be slightly different, since you're utilizing a BindingSource and my examples below bind directly to the DataTable rather than to a BindingSource (you could simply add another overloaded DataBind() method):
>>>>>
>>>>>This CheckBox binds directly to your DataColumn rather than an intermediary property (which can be done too, but takes more code IMHO). This class can be extended to be able to used with other strings, such as T/F, but I’ll leave that as an exercise for the reader:
>>>>>
>>>>>
>>>>>public class BBCheckBoxString : System.Windows.Forms.CheckBox
>>>>>{
>>>>>	protected Binding oBinding = null;
>>>>>
>>>>>	public virtual void DataBind(System.Data.DataTable Data, string Column)
>>>>>	{
>>>>>		this.Checked = false;
>>>>>		this.oBinding = new Binding("Checked", Data, Column);
>>>>>
>>>>>		this.oBinding.Format += new ConvertEventHandler(this.FormatHandler);
>>>>>		this.oBinding.Parse += new ConvertEventHandler(this.ParseHandler);
>>>>>
>>>>>		this.DataBindings.Add(this.oBinding);
>>>>>	}
>>>>>	protected override void FormatHandler(object sender, ConvertEventArgs e)
>>>>>	{
>>>>>		if (e.Value.ToString() == "Y")
>>>>>			e.Value = true;
>>>>>		else
>>>>>			e.Value = false;
>>>>>	}
>>>>>	protected override void ParseHandler(object sender, ConvertEventArgs e)
>>>>>	{
>>>>>		if ((bool)e.Value == true)
>>>>>			e.Value = "Y";
>>>>>		else
>>>>>			e.Value = "N";
>>>>>	}
>>>>>}
>>>>>
>>>>>
>>>>>Then, on your Form or control, to databind that Checkbox, you’d do this:
>>>>>
>>>>>
>>>>>this.MyCheckBox.DataBind(MyTable, "MyYNColumn");
>>>>>
>>>>>
>>>>>Now, a regular CheckBox:
>>>>>
>>>>>
>>>>>public class BBCheckBox : System.Windows.Forms.CheckBox
>>>>>{
>>>>>	#region Declarations
>>>>>	protected Binding oBinding = null;
>>>>>	protected CurrencyManager oCurrency = null;
>>>>>	protected DataTable m_BoundTable = null;
>>>>>	protected string m_BoundColumn = "";
>>>>>	protected bool m_ReadOnly = false;
>>>>>	#endregion
>>>>>
>>>>>	#region Methods
>>>>>	public virtual void DataBind(System.Data.DataTable Data, string column)
>>>>>	{
>>>>>		this.Checked = false;
>>>>>
>>>>>		this.m_BoundTable = Data;
>>>>>		this.m_BoundColumn = column;
>>>>>		this.oBinding = new Binding("Checked", Data, column);
>>>>>
>>>>>		this.oBinding.Format += new ConvertEventHandler(this.FormatHandler);
>>>>>		this.oBinding.Parse += new ConvertEventHandler(this.ParseHandler);
>>>>>
>>>>>		this.DataBindings.Add(this.oBinding);
>>>>>
>>>>>		if (this.BindingContext != null)
>>>>>		{
>>>>>			this.oCurrency = (CurrencyManager)this.BindingContext[this.oBinding.DataSource];
>>>>>			this.oCurrency.PositionChanged += new EventHandler(this.PositionChangedHandler);
>>>>>		}
>>>>>
>>>>>		this.SetEOF();
>>>>>	}
>>>>>	protected virtual void SetEOF()
>>>>>	{
>>>>>		if (this.oCurrency == null)
>>>>>			return;
>>>>>
>>>>>		if (this.oCurrency.Position < 0)
>>>>>			this.Enabled = false;
>>>>>		else 
>>>>>			this.Enabled = !this.ReadOnly;
>>>>>	}
>>>>>	#endregion
>>>>>
>>>>>	#region Events
>>>>>	protected virtual void FormatHandler(object sender, ConvertEventArgs e)
>>>>>	{
>>>>>		if (e.Value == System.DBNull.Value)
>>>>>			e.Value = false;
>>>>>	}
>>>>>	protected virtual void ParseHandler(object sender, ConvertEventArgs e)
>>>>>	{
>>>>>	}
>>>>>	protected virtual void PositionChangedHandler(object sender, System.EventArgs e)
>>>>>	{
>>>>>		this.SetEOF();
>>>>>	}
>>>>>	
>>>>>	#endregion
>>>>>
>>>>>	#region Properties
>>>>>	[DefaultValue(false)]
>>>>>	public bool ReadOnly
>>>>>	{
>>>>>		get { return this.m_ReadOnly; }
>>>>>		set 
>>>>>		{ 
>>>>>			this.m_ReadOnly = value; 
>>>>>			this.Enabled = !value;
>>>>>		}
>>>>>	}
>>>>>
>>>>>	#endregion
>>>>>}
>>>>>
>>>>>
>>>>>HTH,
>>>>>~~Bonnie
>>>>>
>>>>>
>>>>>>Hi,
>>>>>>
>>>>>>I have a column in a table of type nchar(1). The value in this column is "A" or "I". I want to bind this column to a check box and show in the caption of the check box "Active" (for 'A') and "Inactive" for 'I'
>>>>>>
>>>>>>I tried the following binding code but get error:
>>>>>>
>>>>>>
>>>>>>this.chkStatus.DataBindings.Add("Checked", bindingSource1, "Status=='A'");
>>>>>>
>>>>>>
>>>>>>What am I doing wrong? TIA.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform