Danh mục

Binding a Group of Radio Buttons in a Windows Form

Số trang: 6      Loại file: pdf      Dung lượng: 23.08 KB      Lượt xem: 16      Lượt tải: 0    
Hoai.2512

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

[ Team LiB ] Recipe 7.9 Binding a Group of Radio Buttons in a Windows Form Problem You need to bind a field in a database to a radio button and update the database with the radio button selected.
Nội dung trích xuất từ tài liệu:
Binding a Group of Radio Buttons in a Windows Form[ 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 theradio button selected.SolutionUse a hidden TextBox to retrieve and update the field value that corresponds to the radiobutton group. You can use the Tag property of each RadioButton control to hold itscorresponding data field value.The schema of table TBL0709 used in this solution is shown in Table 7-9. Table 7-9. TBL0709 schema Column name Data type Length Allow nulls?Id int 4 NoRadioButtonItemId int 4 NoField1 nvarchar 50 YesThe sample code contains seven event handlers:Form.Load Sets up the sample by creating a DataAdapter with the logic to select all records from table TBL0709 in the sample database and to update changes made back to the database. The DataAdapter is used to fill a DataTable in a new DataSet with the schema and data from TBL0709. The text boxes displaying the data for the fields in the record are bound to the three columns in the table: Id, RadioButtonItemId, and Field1. The RadioButtonItemId TextBox is hidden. The BindingManagerBase is obtained for the TBL0709 table in the DataSet, a handler is attached to manage the PositionChanged event, and that handler is called to position the display on the first record.Update Button.Click Iterates over the group of radio buttons to identify the one selected. The Tag property for the selected radio button is transferred to the hidden bound TextBox for update back to the database.BindingManagerBase.PositionChanged Selects the radio button corresponding to the data field. The code iterates over the group of radio buttons. When the Tag property of a radio button matches the value in the TextBox that is bound to the data field, the radio button is selected.Move First Button.Click Sets the current record of the bound controls to the first record by setting the Position property of the BindingManagerBase object to 0.Move Previous Button.Click Sets the current record of the bound controls to the previous record by decrementing the Position property of the BindingManagerBase object by 1.Move Next Button.Click Sets the current record of the bound controls to the next record by incrementing the Position property of the BindingManagerBase object by 1.Move Last Button.Click Sets the current record of the bound controls to the last record by setting the Position property of the BindingManagerBase object to the total number of records, as returned by the Count property, minus 1.The C# code is shown in Example 7-17.Example 7-17. File: RadioButtonForm.cs// Namespaces, variables, and constantsusing 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 EventHand ...

Tài liệu được xem nhiều: