// 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];
}