以下是设置在FormLoad事件中运行的表单的代码:
private void SoftwareRequestMaint_Load(object sender, EventArgs e)
{
try
{
//Open connection
conn.Open();
//string sqlQuery = "SELECT * FROM Requests; SELECT * FROM Priority; SELECT * FROM Category;" +
// " SELECT * FROM Users; SELECT * FROM System";
string sqlQuery = "SELECT * FROM Requests ORDER BY BriefDesc; " +
"SELECT combined = Description + ' [' + convert(varchar,Priority_ID) + ']', Priority_ID FROM Priority ORDER BY combined; " +
"SELECT combined = Description + ' [' + convert(varchar,Category_ID) + ']', Category_ID FROM Category ORDER BY combined; " +
"SELECT combined = Givname + ' ' + Surname, User_ID FROM Users ORDER BY combined; " +
"SELECT combined = Description + ' [' + convert(varchar,System_ID) + ']', System_ID FROM System ORDER BY combined";
da.SelectCommand = new SqlCommand(sqlQuery, conn);
//due to mulitple SELECTs, set up table mappings other wise need to use Table, Table1, Table2 etc...
da.TableMappings.Add("Table", "Requests");
da.TableMappings.Add("Table1", "Priority");
da.TableMappings.Add("Table2", "Category");
da.TableMappings.Add("Table3", "Users");
da.TableMappings.Add("Table4", "System");
//Go get the data
da.Fill(ds);
//Set relationships
DataColumn colParent =
ds.Tables["Requests"].Columns["Priority"];
DataColumn colChild =
ds.Tables["Priority"].Columns["Priority_ID"];
DataRelation RequestsPriority =
new DataRelation("RequestsPriority", colParent, colChild, false);
ds.Relations.Add(RequestsPriority);
//Bind the dataset to the binding source
bsRequests.DataSource = ds.Tables["Requests"];
bsRequests.Binding.Mode = BindingMode.TwoWay;
//Bind the Textbox's Text property to the Bindingsource's Requests_ID column
uxIDTxt.DataBindings.Add("Text", bsRequests, "Requests_ID");
uxMenuOptTxt.DataBindings.Add("Text", bsRequests, "MenuOpt");
uxProcessNameTxt.DataBindings.Add("Text", bsRequests, "ProcessName");
//Set up data sources for combos
uxBriefDescCbo.DataSource = bsRequests; // ds.Tables["Requests"]; //which one is correct?
uxBriefDescCbo.DisplayMember = "BriefDesc";
uxBriefDescCbo.ValueMember = "Requests_ID";
uxBriefDescCbo.DataBindings.Add("SelectedValue", bsRequests, "Requests_ID");
uxSystemCbo.DataSource = ds.Tables["System"]; //the source of the data for this cbo
uxSystemCbo.DisplayMember = "combined"; //the column name of the table to display
uxSystemCbo.ValueMember = "System_ID"; //the id for that item/record
uxSystemCbo.DataBindings.Add("SelectedValue", bsRequests, "System"); //Bind the cbo to the Navigator's data source, i.e. the binding source
uxPriorityCbo.DataSource = ds.Tables["Priority"];
uxPriorityCbo.DisplayMember = "combined";
uxPriorityCbo.ValueMember = "Priority_ID";
uxPriorityCbo.DataBindings.Add("SelectedValue", bsRequests, "Priority");
uxCategoryCbo.DataSource = ds.Tables["Category"];
uxCategoryCbo.DisplayMember = "combined";
uxCategoryCbo.ValueMember = "Category_ID";
uxCategoryCbo.DataBindings.Add("SelectedValue", bsRequests, "Category");
uxRequestedByCbo.DataSource = ds.Tables["Users"];
uxRequestedByCbo.DisplayMember = "combined";
uxRequestedByCbo.ValueMember = "User_ID";
uxRequestedByCbo.DataBindings.Add("SelectedValue", bsRequests, "RequestedBy");
uxAssignedToCbo.DataSource = ds.Tables["Users"];
uxAssignedToCbo.DisplayMember = "combined";
uxAssignedToCbo.ValueMember = "User_ID";
uxAssignedToCbo.DataBindings.Add("SelectedValue", bsRequests, "AssignedTo");
uxSignedOffByCbo.DataSource = ds.Tables["Users"];
uxSignedOffByCbo.DisplayMember = "combined";
uxSignedOffByCbo.ValueMember = "User_ID";
uxSignedOffByCbo.DataBindings.Add("SelectedValue", bsRequests, "SignedOffBy");
//Bind text boxes to combos
uxRequestDetailsRtb.DataBindings.Add("Text", bsRequests, "Details");
uxItemsChangedRtb.DataBindings.Add("Text", bsRequests, "ItemsChanged");
uxInstallationInstructionsRtb.DataBindings.Add("Text", bsRequests, "InstallInstr");
}
catch (Exception excp)
{
SAPSCommon.Instance.ShowErrorMsg(excp.Message);
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
//Reset dirty data flag after populating controls
dirtyData = false;
}
BTW组合框未排序,即它们的Sorted属性设置为“False”。排序是作为SQL SELECT语句的一部分完成的。
我做错了什么?有什么想法吗?
此致 沃尔特