TwoWay Data Binding Combobox与BindingNavigator

时间:2012-04-30 06:57:01

标签: c# winforms data-binding

好的,所以这是我的困境。我有一个Windows窗体,用于维护SQL表。 SQL表具有指向代码表的外键。我正在使用BindingNavigator和BindingSource,使用文本框,组合框和日期选择器一次一行地显示该表的内容。该表单显示表的键字段BindingNavigator,以及屏幕顶部组框中名为“简要说明”的主表字段之一。该组的目的是允许用户键入ID号,或从组合框中选择一个项目进行查看/编辑。 (由于我是新用户,因此无法发布图像) 我已经设法让表单上显示的数据根据​​BindingNavigator的导航进行更改,即back,fwd等。但是当从Brief Description组合框中选择一个项目时,我无法让表单显示相应的数据。这必须与我的控件绑定有关。可能我只有与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;
}

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

我做错了什么?有什么想法吗?

此致 沃尔特

0 个答案:

没有答案