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)
{
DataGrid gd = (DataGrid)sender;
DataRowView row_selected = gd.SelectedItem as DataRowView;
var windowToOpen = new Window1();
if (gd != null)
{
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();
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();