.NET Forum / Windows Forms / WinForm Data Binding / August 2008
Radio Button Programmatic Binding
|
|
Thread rating:  |
CypherDrive - 11 Aug 2008 06:29 GMT Dear Sir/Madam,
How do I bind a radio button to a Dataset? Please help me out.
This Code generates an error Message.
IsActive is a boolean Field on an SQL Table
Me.rbActive.DataBindings.Add("checked", tblCompanyDataTable, "IsActive")
Thanks in advance for your reply..
Regards,
Cypher Drive
Zhi-Xin Ye [MSFT] - 11 Aug 2008 12:41 GMT Dear Cypher,
As I understand, there's a boolean field in your table, when you bind this field to the Checked property of the RadioButton control, an error occurs.
The code you wrote seems not problem, to help you resolve this problem, I would like to know the following information:
1. What error message did you get? 2. What kind of database did you use? As far as I know there' no bool/boolean type in the SQL Server 2000 or 2005.
I'm looking forward to hearing from you.
Have a nice day!
Sincerely, Zhi-Xin Ye Microsoft Managed Newsgroup Support Team
Delighting our customers is our #1 priority. We welcome your comments and suggestions about how we can improve the support we provide to you. Please feel free to let my manager know what you think of the level of service provided. You can send feedback directly to my manager at: msdnmg@microsoft.com.
================================================== Get notification to my posts through email? Please refer to http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues where an initial response from the community or a Microsoft Support Engineer within 1 business day is acceptable. Please note that each follow up response may take approximately 2 business days as the support professional working with you may need further investigation to reach the most efficient resolution. The offering is not appropriate for situations that require urgent, real-time or phone-based interactions or complex project analysis and dump analysis issues. Issues of this nature are best handled working with a dedicated Microsoft Support Engineer by contacting Microsoft Customer Support Services (CSS) at http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us. ================================================== This posting is provided "AS IS" with no warranties, and confers no rights.
CypherDrive - 13 Aug 2008 01:50 GMT Zhi,
1. I'm using MS SQL Express Edition 2005 2. I'm using the Data Type Bit. 3. The Error Message is ...
Invalid Cast Exception was unhandled Object cannot be cast from DBNull to other types.
Will wait for your response.
Thanks,
Cypher Drive.
> Dear Cypher, > [quoted text clipped - 40 lines] > This posting is provided "AS IS" with no warranties, and confers no > rights. Zhi-Xin Ye [MSFT] - 13 Aug 2008 04:23 GMT Dear Cypher,
Thanks for the information.
The Checked property of the RadioButton control is defined as bool, so only boolean values can be assigned to this property. When there are null values in the IsActive field, they're treated as DbNull objects when performing the data binding. The cast from DbNull type to bool type is invalid, resulting in this error message.
To avoid this error message, you can try two approach:
1. Handle the Format event of the Binding object to assign a boolen value for the Checked property when the ConvertEventArgs.Value is DbNull.Value.
private void Form1_Load(object sender, EventArgs e) { string connstr = Properties.Settings.Default.Database1ConnectionString; SqlConnection conn = new SqlConnection(connstr); string sql = "SELECT * From Table1"; SqlDataAdapter da = new SqlDataAdapter(sql, conn); DataSet ds = new DataSet(); da.Fill(ds, "test");
Binding binding = new Binding("checked", ds.Tables[0], "IsActive"); binding.Format += new ConvertEventHandler(binding_Format); this.radioButton1.DataBindings.Add(binding); }
void binding_Format(object sender, ConvertEventArgs e) { if (e.Value == DBNull.Value) { e.Value = false;//or e.Value = true; } }
2. Change the table definition to not allow null values on the IsActive field.
Please try my suggestions and let me know the result. If anything is unclear, don't hesitate to let me know.
Sincerely, Zhi-Xin Ye Microsoft Managed Newsgroup Support Team
Delighting our customers is our #1 priority. We welcome your comments and suggestions about how we can improve the support we provide to you. Please feel free to let my manager know what you think of the level of service provided. You can send feedback directly to my manager at: msdnmg@microsoft.com.
This posting is provided "AS IS" with no warranties, and confers no rights.
CypherDrive - 13 Aug 2008 06:15 GMT Xin,
I'm sorry but do you have a vb.net 2005 code. I'm not that familiar with C#.
I dont understand this part.
Binding binding = new Binding("checked", ds.Tables[0],
> "IsActive"); > binding.Format += new ConvertEventHandler(binding_Format); [quoted text clipped - 8 lines] > } > } Thanks,
Cypher Drive
> Dear Cypher, > [quoted text clipped - 56 lines] > This posting is provided "AS IS" with no warranties, and confers no > rights. Zhi-Xin Ye [MSFT] - 13 Aug 2008 11:16 GMT Dear Cypher,
I translate the code into VB.NET for your information:
Private Sub Form2_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load Dim connstr As String = My.MySettings.Default.Database1ConnectionString2 Dim conn As SqlConnection = New SqlConnection(connstr) Dim sql As String = "SELECT * From Table1" Dim da As SqlDataAdapter = New SqlDataAdapter(sql, conn) Dim ds As DataSet = New DataSet da.Fill(ds, "test") Dim binding As Binding = New Binding("checked", ds.Tables(0), "IsActive") AddHandler binding.Format, AddressOf Me.binding_Format Me.RadioButton1.DataBindings.Add(binding) End Sub
Private Sub binding_Format(ByVal sender As Object, ByVal e As ConvertEventArgs) If (e.Value Is DBNull.Value) Then e.Value = False 'or e.Value = true; End If End Sub
If need further help on this, please let me know.
Sincerely, Zhi-Xin Ye Microsoft Managed Newsgroup Support Team
Delighting our customers is our #1 priority. We welcome your comments and suggestions about how we can improve the support we provide to you. Please feel free to let my manager know what you think of the level of service provided. You can send feedback directly to my manager at: msdnmg@microsoft.com.
This posting is provided "AS IS" with no warranties, and confers no rights.
Cypher Drive - 29 Aug 2008 09:29 GMT Dear Ye,
Thanks so much.
Regards, CYp Drv.
> Dear Cypher, > [quoted text clipped - 37 lines] > This posting is provided "AS IS" with no warranties, and confers no > rights.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|