public void DataGrid_Data()
{
// 2 second delay before loading DataGrid
var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(.75) };
timer.Start();
timer.Tick += (sender, args) =>
{
timer.Stop();
// Attempt to connect to SQL Server database and populate DataGrid with database tables.
try
{
string connectionString = ("Data Source=WINDOWS-xxxxx\\SQLEXPRESS;Initial Catalog=CustomerRelations;Integrated Security=True;");
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("SELECT [hb_Disputes].[DSP_ID], [hb_disputes].[ACCOUNT], [Users].[TX_EMPLOYEE], [hb_CsrNames].[NM_USER], [hb_disputes].[CUST_NAME],[hb_disputes].[PREM_ADDR], [hb_status].[Status], [hb_disputes].[OPENED], [hb_disputes].[DEADLINE], [hb_disputes].[DATERSLVD], [hb_rpttype].[ReportType], [hb_ratetype].[RateType], [hb_Disputes].[FR_DT_FIRSTREV], [hb_Disputes].[FR_TS_LATESTUPD], [hb_Disputes].[COMMENT], [hb_Disputes].[FR_DSP_CLSF], [hb_Disputes].[FR_CUST_CNTCT], [hb_Disputes].[FR_WRK_REQ], [hb_Disputes].[FR_OPN_ERR], [hb_Disputes].[FR_SO_TP], [hb_Disputes].[FR_SO_DTLS], [hb_Disputes].[FR_SO_DT_WNTD], [hb_Disputes].[FR_SO_ISSD_BY], [hb_Disputes].[FR_CMMNT] FROM [hb_disputes]" +
" LEFT JOIN [Users] ON [hb_disputes].[ASSGNTO] = [Users].[KY_USER_ID] LEFT JOIN [hb_CsrNames] ON [hb_disputes].[WFMUSER] = [hb_CsrNames].[KY_USER_ID] LEFT JOIN [hb_status] ON [hb_disputes].[STATUS] = [hb_status].[STSID] LEFT JOIN [hb_rpttype] ON [hb_disputes].[RPTTYPE] = [hb_rpttype].[RPTID] LEFT JOIN [hb_ratetype] ON [hb_disputes].[REV_CLS] = [hb_ratetype].[RTID]", connection);
connection.Open();
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
connection.Close();
dtGrid.DataContext = dt;
// Focus in on line 1 of the data grid on UserControl load
// So it can be shown in the key information side column
dtGrid.Focus();
dtGrid.SelectedIndex = 0;
}
catch
{
MessageBox.Show("Database connection is not available at this time. Please contact your database administrator ");
}
};
}
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_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
windowToOpen.cmb_UtilityRptTyp.SelectedValuePath = row_selected["ReportType"].ToString();
windowToOpen.Show();
}
else
{
return;
}
}
Внутренний код Window1 Combobox
public void txt_UtilityRptTyp_Loaded(object sender, RoutedEventArgs e)
{
{
SqlConnection connection = new SqlConnection("Data Source=WINDOWS-xxxxx\\SQLEXPRESS;Initial Catalog=CustomerRelations;Integrated Security=True;");
try
{
connection.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
try
{
SqlDataAdapter Status_data = new SqlDataAdapter("SELECT * 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());
}
}
}
Windowmal Comobobox xmal code
<ComboBox
x:Name="cmb_UtilityRptTyp"
Width="180"
Height="26"
Margin="5,0,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Bottom"
BorderBrush="DarkGray"
BorderThickness="1"
Foreground="Black"
Loaded="txt_UtilityRptTyp_Loaded">
<ComboBox.SelectedValuePath>
<Binding Path="RPTID" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
</ComboBox.SelectedValuePath>
<ComboBox.DisplayMemberPath>
<Binding Path="ReportType" Mode="TwoWay"/>
</ComboBox.DisplayMemberPath>
У меня есть событие двойного щелчка на моей DataGrid, которое откроет новый Окно (Окно 1) и введите значения из сетки данных в текстовые поля и комбинированные списки в новом окне. Текстовые поля заполняются нормально, однако, с помощью (windowToOpen.cmb_UtilityRptTyp.SelectedValuePath = row_selected ["ReportType"]. ToString ();) используя (SelectedValuePath), он показывает список элементов, а также показывает, что элемент выбран, но это не так. элемент, который был выбран в строке DataGrid. Я полагаю, что это показывает список элементов и выбирает имя из этого списка.
Я пытаюсь дать пользователю возможность выбрать элемент из строки DataGrid и сохранить изменения выбора в выпадающих списках и сохранить их в базе данных SQL Server.