[ Team LiB ] |
Recipe 7.11 Populating a Windows Forms ComboBoxProblemYou need to populate a ComboBox from a database, bind a field in a database to the ComboBox so that the correct value is selected from the list, and use the value selected in the ComboBox to update the database. SolutionYou must:
The schema of table TBL0711 used in this solution is shown in Table 7-11.
The schema of table TBL0711_ComboBoxSource used in this solution is shown in Table 7-12.
The sample code contains seven event handlers:
The C# code is shown in Example 7-19. Example 7-19. File: ComboBoxForm.cs// Namespaces, variables, and constants using System; using System.Configuration; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; private const String TABLENAME = "TBL0711"; private const String TABLENAME_COMBOBOXSOURCE = "TBL0709_ComboBoxSource"; private const String RELATIONNAME = "REL0711"; private DataSet ds; private SqlDataAdapter da; private BindingManagerBase bm; // . . . private void ComboBoxForm_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, ComboBoxItemId, Field1 FROM " + TABLENAME; String updateCommand = "UPDATE " + TABLENAME + " " + "SET ComboBoxItemId = @ComboBoxItemId, 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("@ComboBoxItemId", SqlDbType.Int, 0, "ComboBoxItemId"); 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); // Create and fill the schema for the ComboBox table. String sqlText = "SELECT ComboBoxItemId, Description FROM " + TABLENAME_COMBOBOXSOURCE; SqlDataAdapter daCB = new SqlDataAdapter(sqlText, da.SelectCommand.Connection); DataTable comboBoxSourceTable = new DataTable(TABLENAME_COMBOBOXSOURCE); daCB.FillSchema(comboBoxSourceTable, SchemaType.Source); // Sdd the instructions for the user as the first element. comboBoxSourceTable.Rows.Add(new object[] {-1, "<Select>"}); // Fill the rest of the data for the ComboBox. daCB.Fill(comboBoxSourceTable); // Add the ComboBox source table to the DataSet. ds.Tables.Add(comboBoxSourceTable); // Relate the parent and ComboBox tables. ds.Relations.Add(new DataRelation(RELATIONNAME, ds.Tables[TABLENAME_COMBOBOXSOURCE].Columns["ComboBoxItemId"], ds.Tables[TABLENAME].Columns["ComboBoxItemId"], true)); // Set up the ComboBox with the DataSet. comboBox.DataSource = ds.Tables[TABLENAME_COMBOBOXSOURCE]; comboBox.ValueMember = "ComboBoxItemId"; comboBox.DisplayMember = "Description"; // Bind all of the controls to the DataSet. idTextBox.DataBindings.Add("Text", ds, TABLENAME + ".Id"); comboBox.DataBindings.Add("SelectedValue", ds, TABLENAME + ".ComboBoxItemId"); field1TextBox.DataBindings.Add("Text", ds, TABLENAME + ".Field1"); // Get the binding manager base for the parent table. bm = BindingContext[ds, TABLENAME]; } private void updateButton_Click(object sender, System.EventArgs e) { // End the current update and update the record using the DataAdapter. bm.EndCurrentEdit( ); da.Update(ds.Tables[TABLENAME]); } private void comboBox_SelectionChangeCommitted(object sender, System.EventArgs e) { resultTextBox.Text="SelectedIndex = " + comboBox.SelectedIndex + Environment.NewLine + "SelectedValue = " + comboBox.SelectedValue + Environment.NewLine + "SelectedText = " + ((DataRowView)comboBox.Items[comboBox.SelectedIndex]) ["Description"]; } 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; } DiscussionCombo boxes are most commonly used to browse data, enter new data, or edit existing data in a data source. There are two ways to fill a ComboBox: either use the Add( ) method or bind the ComboBox to a data source. The Windows Forms ComboBox control has three properties that are used to control data binding to an ADO.NET data source. These are described in Table 7-13.
The DataBindings property of a control exposes the ControlBindingsCollection object for the control. The Add( ) method of this collection adds a Binding to the collection. The overload of the Add( ) method used in the solution takes three arguments:
In the solution, as shown in this excerpt, the SelectedValue property of the ComboBox is bound to the ComboBoxItemId field in the destination table TBL0709 in the DataSet ds: comboBox.DataBindings.Add("SelectedValue", ds, TABLENAME + ".ComboBoxItemId"); The SelectionChangeCommitted event raised by the ComboBox occurs when the item selected is changed and the change is committed. The event handler receives an EventArgs argument. You can use the handler for the event to get the new value of the ComboBox once it has been changed. |
[ Team LiB ] |