Binding a Group of Radio Buttons in a Windows Form - Pdf 76

[ 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.
Solution
Use 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
.
Table 7-9. TBL0709 schema
Column name Data type Length Allow nulls?
Id int 4 No
RadioButtonItemId int 4 No
Field1 nvarchar 50 Yes
The 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


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,

{
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)

3. Bind the Text property of the TextBox to the data source:
4. radioButtonItemIdTextBox.DataBindings.Add("Text", ds, TABLENAME +
".RadioButtonItemId");
5. Get the BindingManagerBase for the DataTable using the indexer (Item property


Nhờ tải bản gốc
Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status