Data binding using checkbox
-
Hi all, I have a question would like to ask regards to the complex data binding using checkbox control. My situation was my data types at SQL database was char data types and it holds only 'Y' and 'N' value. Now I would like to bind the char datatypes 'Y'/'N' to the checkbox control. When the values hold for 'Y' and checkbox will be checked. Otherwise, it was in uncheck mode. If the values holds for other data values beside 'Y' / 'N', an intermediate checkbox value will be holded. whatever method being tried before, it can't bind correctly onto the checkbox control, is there any method I can bind it correctly using char data types by using checkbox control?:( Steven Leong
-
Hi all, I have a question would like to ask regards to the complex data binding using checkbox control. My situation was my data types at SQL database was char data types and it holds only 'Y' and 'N' value. Now I would like to bind the char datatypes 'Y'/'N' to the checkbox control. When the values hold for 'Y' and checkbox will be checked. Otherwise, it was in uncheck mode. If the values holds for other data values beside 'Y' / 'N', an intermediate checkbox value will be holded. whatever method being tried before, it can't bind correctly onto the checkbox control, is there any method I can bind it correctly using char data types by using checkbox control?:( Steven Leong
Hi, This can be resolved using Format/Parse handlers. Here the code :
Binding YesNoBinding = new Binding("Checked", yourDatasource, "YourPropertyName", false, DataSourceUpdateMode.OnPropertyChanged); YesNoBinding.Format += new ConvertEventHandler(YesNoBinding_Format); YesNoBinding.Parse += new ConvertEventHandler(YesNoBinding_Parse); yourCheckBox.DataBindings.Add(YesNoBinding);
And here the code for handlers :void YesNoBinding_Parse(object sender, ConvertEventArgs e) { if ((bool)e.Value == true) e.Value = "Y"; else e.Value = "N"; } void YesNoBinding_Format(object sender, ConvertEventArgs e) { if ((string) e.Value == "Y") e.Value = true; else e.Value = false; }
HTH. Hayder Marzouk