Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Binding Checkbox to nchar column
Message
From
26/01/2010 13:25:29
 
 
To
26/01/2010 13:07:46
General information
Forum:
ASP.NET
Category:
Forms
Environment versions
Environment:
C# 2.0
Miscellaneous
Thread ID:
01445996
Message ID:
01446017
Views:
26
>>1. I have noticed that both of your checkbox classes (BBCheckBoxString and BBCheckBox) are designed to be bound to a string column. Do I understand that I would need to create another class - e.g. XXCheckBoxBit - to bind a checkbox to a bit type column?
>
>No, the second one (BBCheckBox) is your typical CheckBox, designed to be bound to a boolean value (bit in the database), which most of the time you want to do. I'm not sure what gave you the impression it was for a string datatype.
>
>The BBCheckBoxString is mainly for handling legacy database columns that had the bad design of using a char instead of a bit. <g>
>
>>2. I don't understand a lot in your code but will review it and hopefully learn a lot (sure of that). But I noticed that you use the "m_" naming convention. I also saw it in the book on WinForms binding I have and I like it. This means 2 votes for this convention. But I also noticed that you don't name everything with prefix "m_". What is your approach on where to use m_ and where to use "o"?
>
>Typically I use "m_" *only* for private/protected members that I intend to expose as public properties. For objects where I cannot come up with any clear-cut prefix (or don't feel like using a prefix), I simply use "o". An example would be that Binding member that I called oBinding. Seems silly to use a prefix on that (what would it be? bindBinding? bndBinding? ... dunno, I like oBinding better).

Hi,
No axe to grind whatsoever but I'm curious as to why you use Hungarian notation at all. MS guidelines are against it (http://msdn.microsoft.com/en-us/library/ms229045.aspx) and most code samples don't use it. I see it most often used by programmers who have come from a non-strongly typed language like VFP. I sometimes find myself doing it out of habit but when I think about it I don't see the benefit - the actual *exact* type of the object is immediately obvious by hovering over it - and a mis-placed prefix can be misleading......

Best,
Viv

>
>~~Bonnie
>
>
>>
>>
>>>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.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform