Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have DataGrid that is populated but an SQL Server database in my MainWindow. I have an event that is fired when the user double clicks on the row to open a new window (Window 1) and populate Textboxes and ComboBoxes with the information from the row.

private void dtGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{

    // User double clicks on DataGrid Row
    // Open new Window
    // Populate selected textboxes with selected datarow
    DataGrid gd = (DataGrid)sender;
    DataRowView row_selected = gd.SelectedItem as DataRowView;

    var windowToOpen = new Window1();

    if (gd != null)
    {
        // Textboxes
        windowToOpen.txt_RowRecrd.Text = row_selected["DSP_ID"].ToString();
        windowToOpen.txt_acctnumber.Text = row_selected["ACCOUNT"].ToString();
        windowToOpen.txt_analyst.Text = row_selected["TX_EMPLOYEE"].ToString();
        windowToOpen.txt_custname.Text = row_selected["CUST_NAME"].ToString();
        windowToOpen.txt_address.Text = row_selected["PREM_ADDR"].ToString();
        windowToOpen.txt_Status.Text = row_selected["Status"].ToString();
        windowToOpen.txt_opened.Text = row_selected["OPENED"].ToString();
        windowToOpen.txt_deadline.Text = row_selected["DEADLINE"].ToString();
        windowToOpen.txt_DaysOpen.Text = row_selected["DaysOpen"].ToString();
        windowToOpen.txt_DateResolved.Text = row_selected["DATERSLVD"].ToString();
        windowToOpen.txt_revcls.Text = row_selected["RateType"].ToString();
        windowToOpen.txt_WFMissuedBy.Text = row_selected["NM_USER"].ToString();
        windowToOpen.txt_firstreview.Text = row_selected["FR_DT_FIRSTREV"].ToString();
        windowToOpen.txt_Latestupdate.Text = row_selected["FR_TS_LATESTUPD"].ToString();
        windowToOpen.txt_reviewNotes.Text = row_selected["FR_CMMNT"].ToString();
        windowToOpen.txt_ResolutionNotes.Text = row_selected["COMMENT"].ToString();



        // ComboBoxes

        foreach (DataRowView row in dtGrid.SelectedItems)
        {
            windowToOpen.cmb_UtilityRptTyp.Items.Clear();
            windowToOpen.cmb_UtilityRptTyp.SelectedIndex = windowToOpen.cmb_UtilityRptTyp.Items.Add(row["RPTTYPE"]);
        }

        windowToOpen.Show();
    }
    else
    {
        return;
    }

}


In Window 1, I have back-end code to my ComboBox which populates the item list from the same SQL Server database when the Window is loaded.

public void txt_UtilityRptTyp_Loaded(object sender, RoutedEventArgs e)
{
    {
        SqlConnection connection = new SqlConnection("Data Source=WINDOWS-B1AT5HC\\SQLEXPRESS;Initial Catalog=CustomerRelations;Integrated Security=True;");

        try
        {
            connection.Open();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        try
        {
            SqlDataAdapter Status_data = new SqlDataAdapter("SELECT RPTID, ReportType FROM [hb_RptType]", connection);
            DataSet ds = new DataSet();
            Status_data.Fill(ds, "t");

            cmb_UtilityRptTyp.ItemsSource = ds.Tables["t"].DefaultView;
            cmb_UtilityRptTyp.DisplayMemberPath = "ReportType";
            cmb_UtilityRptTyp.SelectedValuePath = "RPTID";

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
}


However, with the code I have written
windowToOpen.cmb_UtilityRptTyp.SelectedIndex = windowToOpen.cmb_UtilityRptTyp.Items.Add(row["RPTTYPE"]);
I've been only able to show the numeric value from the DataGrid column but have not been able to bind it correctly to the comobox to show the item list text.

What I have tried:

I've tired using
windowToOpen.cmb_UtilityRptTyp.SelectedItem = row_selected["RPTTYPE"].ToString();


<pre>                windowToOpen.cmb_UtilityRptTyp.SelectedValue = row_selected["RPTTYPE"].ToString();
Posted
Updated 15-Apr-20 10:43am

1 solution

You can define public variables or an object that you want to use in the second form as define from the first form. In your case you could pass an object (datagridrow) or the id the the row from the database or multiple values depending on what you are trying to achieve.

public partial class Form2: Form
{
    public string x;    
   
    public Form2 (string StringFromForm1)
    {
        InitializeComponent();
        x = StringFromForm1;   
    }

    //code to do with value
}

In Form1:

Form2 form2 = new Form2("SomeValue");
form2.Show();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900