[ Team LiB ] |
Recipe 7.9 Binding a Group of Radio Buttons in a Windows FormProblemYou need to bind a field in a database to a radio button and update the database with the radio button selected. SolutionUse a hidden TextBox to retrieve and update the field value that corresponds to the radio button group. You can use the Tag property of each RadioButton control to hold its corresponding data field value. The schema of table TBL0709 used in this solution is shown in Table 7-9.
The sample code contains seven event handlers:
The C# code is shown in Example 7-17. Example 7-17. File: RadioButtonForm.cs// Namespaces, variables, and constants using System; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; private const String TABLENAME = "TBL0709"; private DataSet ds; private SqlDataAdapter da; private BindingManagerBase bm; // . . . private void RadioButtonForm_Load(object sender, System.EventArgs e) { // Create the DataSet. ds = new DataSet( ); // Create the select and update commands for the DataAdapter. String selectCommand = "SELECT Id, RadioButtonItemId, Field1 FROM " + TABLENAME; String updateCommand = "UPDATE " + TABLENAME + " " + "SET RadioButtonItemId=@RadioButtonItemId, Field1=@Field1 " + "WHERE Id=@Id"; // Create the DataAdapter. da = new SqlDataAdapter(selectCommand, ConfigurationSettings.AppSettings["Sql_ConnectString"]); da.UpdateCommand = new SqlCommand(updateCommand, da.SelectCommand.Connection); da.UpdateCommand.CommandType = CommandType.Text; da.UpdateCommand.Parameters.Add("@Id", SqlDbType.Int, 0, "Id"); da.UpdateCommand.Parameters.Add("@RadioButtonItemId", SqlDbType.Int, 0, "RadioButtonItemId"); da.UpdateCommand.Parameters.Add("@Field1", SqlDbType.NVarChar, 50, "Field1"); // Retrieve the data and schema for the table. da.FillSchema(ds, SchemaType.Source, TABLENAME); da.Fill(ds, TABLENAME); // Bind all of the controls, including hidden text box, to the DataSet. idTextBox.DataBindings.Add("Text", ds, TABLENAME + ".Id"); radioButtonItemIdTextBox.DataBindings.Add("Text", ds, TABLENAME + ".RadioButtonItemId"); radioButtonItemIdTextBox.Visible = false; field1TextBox.DataBindings.Add("Text", ds, TABLENAME + ".Field1"); // Get the binding manager base for the table. bm = BindingContext[ds, TABLENAME]; // Update the correct radio button in response to each record // reposition. bm.PositionChanged += new EventHandler(bm_PositionChanged); // Update the display for the first record. bm_PositionChanged(null, null); } private void updateButton_Click(object sender, System.EventArgs e) { // Retrieve the selected radio button based on the value in the // tag field to the hidden text box. foreach(RadioButton rb in radioButtonGroupBox.Controls) { if (rb.Checked) { radioButtonItemIdTextBox.Text = rb.Tag.ToString( ); break; } } // End the current update and update the record using the DataAdapter. bm.EndCurrentEdit( ); da.Update(ds.Tables[TABLENAME]); } private void bm_PositionChanged(Object sender, EventArgs e) { // Refresh the checked radio button when the current record changes. foreach(RadioButton rb in radioButtonGroupBox.Controls) { if (rb.Tag.ToString( ) == radioButtonItemIdTextBox.Text) { rb.Checked = true; break; } } } private void moveFirstButton_Click(object sender, System.EventArgs e) { bm.Position = 0; } private void movePreviousButton_Click(object sender, System.EventArgs e) { bm.Position -= 1; } private void moveNextButton_Click(object sender, System.EventArgs e) { bm.Position += 1; } private void moveLastButton_Click(object sender, System.EventArgs e) { bm.Position = bm.Count - 1; } DiscussionWhile a RadioButton control can be set to simple-bind to data, there is no way to bind a group of RadioButton controls to a data source. Binding a single radio button to a data source isn't a particularly common requirement—nor is it particularly useful—since radio buttons are normally used in groups to allow an option to be selected from a group of mutually exclusive options. Web Forms provides a RadioButtonList control that works as a parent control to a collection of radio button list items. It inherits from the ListControl class and as a result works similarly to the ListBox and DropDownList controls. There is no RadioButtonList control available for Windows Forms applications. For more information about the RadioButtonList class, see the MSDN Library. Radio button data binding can be simulated in a Windows Form application by following these steps:
|
[ Team LiB ] |