Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > CheckBox (databinding?) problem
 

CheckBox (databinding?) problem

Hi All!

In my application I creating datatable with one row which contains data to form "where" for query. When I creating this databale I am also creating at runtime appropriate controls and bind them to datatableso user can enter desired values for search. Until datatable does not have Boolean type column in its structure everything works fine. But as soon I am using Boolean datatype in datatable and CheckBox to get value from user I get into problem - databinding stops working at all. It means that TextBox binded to same datable also not passing down value.

In this piece I am having just string column and bind textbox and it works fine

Code Snippet

DataTable dtFilter = new DataTable();

DataColumn dc = new DataColumn();

dc.ColumnName = "TextColumn";

dc.DataType = typeof(String)

dc.AllowDBNull = true;

dtFilter.Columns.Add(dc);

//Create TextBox and bind it to datatable

cl = new TextBox();

cl.DataBindings.Add("Text", this.dtFilter, TextColumn, true, DataSourceUpdateMode.OnValidation, string.Empty);

// Add TextBox to panel on form

this.tblPanel.Controls.Add(cl);

DataRow drEmptyFilter = dtFilter.NewRow();

dtFilter.Rows.Add(drEmptyFilter);

dtFilter.AcceptChanges();

Code above works fine, when user enters something in textbox it ends up in datatable

But if add code below none of the controls arepassing data to datatable

Code Snippet

//Adding Boolean column in datatable

DataColumn dc1 = new DataColumn();

dc1.ColumnName = "BooleanColumn";

dc1.DataType = typeof(Boolean)

dc1.AllowDBNull = true;

dtFilter.Columns.Add(dc1);

cl = new CheckBox();

//cl.DataBindings.Add("CheckState", this.dtFilter,"BooleanColumn"); //Short version

cl.DataBindings.Add("CheckState", this.dtFilter, "BooleanColumn", false, DataSourceUpdateMode.OnValidation, null); //long version

I would appreciateANY thoughts.Thanks.

likhitn  Thursday, March 13, 2008 3:13 AM

Hi Zhi-xin Ye

Thank you for your answer. After I had noanswers/comments ( I asked in couple places) I started to build control sample to post it (thought that I could not give enough information to get advice on forums) and get this line working

Code Snippet

cl.DataBindings.Add("Checked", dtFilter, "BooleanField", true, DataSourceUpdateMode.OnValidation );

Thanks again, my lesson with DataBinding that couple parameters shouldbe in some certain composition. If you can put some light on this hidden mechanics It would be really good as a lot of people (turns out include myself) using parameters in DataBinding without good understanding which parameters, when and how use them to get desired result.

Thanks

likhtin

likhitn  Wednesday, March 19, 2008 1:22 PM

Hi

You should bind the CheckBox.Checked property if you disable the formatting(3rd parameter set to FALSE), or you can enable the formatting by set the 3rd parameter to TRUE, something as following would work

.checkBox1.DataBindings.Add("Checked", dt, "status",

false, DataSourceUpdateMode.OnValidation, null);

.checkBox1.DataBindings.Add("CheckState", dt, "status",

true, DataSourceUpdateMode.OnValidation,null);



Best Regards,
Zhi-xin Ye.

Zhi-Xin Ye  Wednesday, March 19, 2008 8:17 AM

Hi Zhi-xin Ye

Thank you for your answer. After I had noanswers/comments ( I asked in couple places) I started to build control sample to post it (thought that I could not give enough information to get advice on forums) and get this line working

Code Snippet

cl.DataBindings.Add("Checked", dtFilter, "BooleanField", true, DataSourceUpdateMode.OnValidation );

Thanks again, my lesson with DataBinding that couple parameters shouldbe in some certain composition. If you can put some light on this hidden mechanics It would be really good as a lot of people (turns out include myself) using parameters in DataBinding without good understanding which parameters, when and how use them to get desired result.

Thanks

likhtin

likhitn  Wednesday, March 19, 2008 1:22 PM
Zhi-Xin Ye  Wednesday, March 19, 2008 1:34 PM

You can use google to search for other answers

Custom Search

More Threads

• Octagone Style buttons and Form
• Bindinglist with sort does not fire PropertyChanged event.
• Mailing Concept
• Datagridview Rows??
• How to use an IF statement to filter values in a DataSet column?
• Problems using DataSource, DisplayMember and ValueMember with ComboBox
• Simple ADO.NET 2.0 Application: Prompting the user to SAVE his changes before exiting...
• Get the properties of an object into a drop-down list?
• Error when applying filter to datagrid with DataView as datasource
• DataGridView DoubleClick event does not get the correct row after sorting columns (ColumnHeaderMouseClick event)