3

好的,这就是我的困境。我有一个用于维护 SQL 表的 windows 窗体。SQL 表具有指向代码表的外键。我正在使用 BindingNavigator 和 BindingSource 使用文本框、组合框和日期选择器在表单中一次一行地显示该表的内容。该表单在屏幕顶部的组框中显示表的关键字段 BindingNavigator 和称为“Brief Description”的主表字段之一。该组的目的是允许用户键入一个 ID 号,或从组合框中选择一个项目来查看/编辑。(由于我是新用户,无法发布图片)。
我已经设法让表单上显示的数据根据​​ BindingNavigator 的导航进行更改,即返回、前进等,但是当从简要说明组合框中选择一个项目时,我无法让表单显示适当的数据。这一定与我的控件绑定有关。可能我只有一个与 BindingSource 的 Oneway 绑定,而不是 TwoWay 绑定,但我不知道如何使其成为 TwoWay 绑定。

这是设置在 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;
}

顺便说一句,组合框是未排序的,即它们的 Sorted 属性设置为“False”。排序是作为 SQL SELECT 语句的一部分完成的。

我究竟做错了什么?有任何想法吗?

问候,沃尔特

4

0 回答 0