Combining TwoWay Data Binding with BindingNavigator

OK, so this is my dilemma. I have a window form that will be used to support an SQL table. The SQL table has foreign keys pointing to code tables. I use BindingNavigator and BindingSource to display the contents of this table one row at a time in the form using text fields, comboboxes, and datepickers. The form displays the table key field, the BindingNavigator, and one of the main fields of the table called the “Short Description” in the group box at the top of the screen. The purpose of this group is to allow the user to enter an identification number or select an item from the list with a list for viewing / editing. (It is not possible to send images because I am a new user).
I was able to display the data displayed on the form, depending on the navigation of the BindingNavigator, i.e. Back, fwd, etc. But I can’t get a form to display the relevant data when I select an item from the "Short Description" drop-down list. This should be due to the binding of my controls. It is possible that I have an Oneway binding to a BindingSource, not a TwoWay binding, but I cannot figure out how to do this with a TwoWay binding.

Here is the code that sets up the form that runs in the FormLoad event:

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 Text property to the Bindingsource 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 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 The streams are unsorted, i.e. their Sorted property is set to False. Sorting is performed as part of the SQL SELECT statements.

What am I doing wrong? Any ideas?

Regards, Walter

+3
source share

All Articles