Здравствуйте, у меня есть событие двойного щелчка мыши для моего DataGrid
в моем окне Parent
, которое открывает окно Child
и заполняет некоторые Textboxes
и Comboboxes
У меня также есть окно Событие закрытия для моего Child
окна, в котором сохраняются изменения, внесенные в базу данных SQL Server.
Я пытаюсь получить DataGrid
refre sh, когда мое окно Child
было закрыто. Пожалуйста помоги!
Код DataGrid родительского окна:
public partial class DataGrid_HBD : UserControl
{
public DataGrid_HBD()
{
InitializeComponent();
SearchButtons_Load();
DataGridData();
// 2 Seconds Timer before connecting to the Database.
// This improves UI rendering on button click
}
/// <summary>
/// Loading Data Grid
/// </summary>
public void DataGridData()
{
// Attemption to connect to SQL Server database and populate DataGrid with database tables.
try
{
string connectionString = ("Data Source=WINDOWS-B1AT5HC\\SQLEXPRESS;Initial Catalog=CustomerRelations;Integrated Security=True;");
SqlConnection connection = new SqlConnection(connectionString);
// Review SQL queries for security vulnerabilities
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_disputes].[PRMCAUSE], [hb_disputes].[RPTTYPE], [hb_ratetype].[RateType], [hb_Disputes].[FR_DT_FIRSTREV], [hb_Disputes].[FR_TS_LATESTUPD], [hb_Disputes].[COMMENT], [hb_Disputes].[FR_DSP_CLSF], [hb_Disputes].[FINADJ], [hb_Disputes].[CALLRSN], [hb_Disputes].[SECCAUSE], [hb_Disputes].[MTR], [hb_Disputes].[SERTYPE], [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], DATEDIFF(day,[OPENED], [DEADLINE]) AS DaysOpen 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_ratetype] ON [hb_disputes].[REV_CLS] = [hb_ratetype].[RTID] Where [hb_disputes].[STATUS]= 3 AND [Users].[TX_EMPLOYEE] = '" + Environment.UserName + "'", connection);
// Review SQL queries for security vulnerabilities
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
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
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_DaysOpen.Text = row_selected["DaysOpen"].ToString();
windowToOpen.txt_revcls.Text = row_selected["RateType"].ToString();
windowToOpen.txt_WFMissuedBy.Text = row_selected["NM_USER"].ToString();
windowToOpen.txt_ReviewNotes.Text = row_selected["FR_CMMNT"].ToString();
windowToOpen.txt_ResolutionNotes.Text = row_selected["COMMENT"].ToString();
windowToOpen.DatePicker_ScheduledFor.Text = row_selected["FR_SO_DT_WNTD"].ToString();
// Date Open Parse is needed for ShortDate format.
if (row_selected["OPENED"] != null)
{
DateTime dateTime;
if (DateTime.TryParse(row_selected["OPENED"].ToString(), out dateTime))
{
windowToOpen.txt_opened.Text = dateTime.ToShortDateString();
}
else
{
windowToOpen.txt_opened.Text = ""; //assign default value
}
}
// Deadline Parse is needed for ShortDate format.
if (row_selected["DEADLINE"] != null)
{
DateTime dateTime;
if (DateTime.TryParse(row_selected["DEADLINE"].ToString(), out dateTime))
{
windowToOpen.txt_deadline.Text = dateTime.ToShortDateString();
}
else
{
windowToOpen.txt_deadline.Text = ""; //assign default value
}
}
// Date Resolved Parse is needed for ShortDate format.
if (row_selected["DATERSLVD"] != null)
{
DateTime dateTime;
if (DateTime.TryParse(row_selected["DATERSLVD"].ToString(), out dateTime))
{
windowToOpen.txt_DateResolved.Text = dateTime.ToShortDateString();
}
else
{
windowToOpen.txt_DateResolved.Text = ""; //assign default value
}
}
windowToOpen.Show();
}
else
{
return;
}
}
А вот мое закрывающее событие в моем дочернем окне:
private void Window_HBD_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=WINDOWS-B1AT5HC\\SQLEXPRESS;Initial Catalog=CustomerRelations;Integrated Security=True;");
try
{
SqlCommand cmd = new SqlCommand("UPDATE [hb_Disputes] SET FR_DSP_CLSF=@FR_DSP_CLSF, FR_CUST_CNTCT=@FR_CUST_CNTCT, FR_WRK_REQ=@FR_WRK_REQ, FR_OPN_ERR=@FR_OPN_ERR, FR_SO_TP=@FR_SO_TP, FR_SO_DTLS=@FR_SO_DTLS, FR_CMMNT=@FR_CMMNT, FR_SO_DT_WNTD=@FR_SO_DT_WNTD, FINADJ=@FINADJ, SERTYPE=@SERTYPE, CALLRSN=@CALLRSN, SECCAUSE=@SECCAUSE, MTR=@MTR, RPTTYPE=@RPTTYPE, PRMCAUSE=@PRMCAUSE, COMMENT=@COMMENT, FR_TS_LATESTUPD=@FR_TS_LATESTUPD WHERE DSP_ID=@DSP_ID", con);
cmd.Parameters.AddWithValue("@DSP_ID", txt_RowRecrd.Text);
// Second Row
cmd.Parameters.AddWithValue("@FR_DSP_CLSF", cmb_DisputeClassification.SelectedValue);
cmd.Parameters.AddWithValue("@FR_CUST_CNTCT", cmb_CustomerContact.SelectedValue);
cmd.Parameters.AddWithValue("@FR_WRK_REQ", cmb_requestedwork.SelectedValue);
cmd.Parameters.AddWithValue("@FR_OPN_ERR", cmb_OpenInError.SelectedValue);
cmd.Parameters.AddWithValue("@FR_SO_TP", cmb_ServiceOrderType.SelectedValue);
cmd.Parameters.AddWithValue("@FR_SO_DTLS", cmb_ServiceOrderDetails.SelectedValue);
cmd.Parameters.AddWithValue("@FR_CMMNT", txt_ReviewNotes.Text);
cmd.Parameters.AddWithValue("@FR_SO_DT_WNTD", DatePicker_ScheduledFor.Text);
// Third Row
cmd.Parameters.AddWithValue("@RPTTYPE", cmb_UtilityRptTyp.SelectedValue);
cmd.Parameters.AddWithValue("@FINADJ", cmb_FinancialAdjustment.SelectedValue);
cmd.Parameters.AddWithValue("@SERTYPE", cmb_ServiceTypeAdjustment.SelectedValue);
cmd.Parameters.AddWithValue("@CALLRSN", cmb_InitialCallReason.SelectedValue);
cmd.Parameters.AddWithValue("@PRMCAUSE", cmb_PrimCause.SelectedValue);
cmd.Parameters.AddWithValue("@SECCAUSE", cmb_UnderlyingCause.SelectedValue);
cmd.Parameters.AddWithValue("@MTR", cmb_MeterIssue.SelectedValue);
cmd.Parameters.AddWithValue("@COMMENT", txt_ResolutionNotes.Text);
cmd.Parameters.AddWithValue("@FR_TS_LATESTUPD", DateTime.Now);
//cmd.Parameters.Add("@FR_TS_LATESTUPD", SqlDbType.DateTime).Value = DateTime.UtcNow;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}