I have a form (frmPrintReq) with a datagrid on it. When the user double clicks a row, it opens a second form (frmShowReq) with the information of that row. The user can then update the information. When the user clicks the update button of form2, how can I then update the dataset (dsNew1) of the datagrid (dgNew) on form1 and refresh the datagrid. Here is the code from the first form that opens the second form:
and here is the code from the update button on form2Code:private void frmPrintReq_Load(object sender, EventArgs e) { sqlDataAdapter1.Fill(dsNew1, "MyImages"); } private void dgNew_DoubleClick(object sender, EventArgs e) { if (this.dgNew.CurrentRowIndex.ToString() == "-1") { MessageBox.Show("First select a print request", "No request selected.", MessageBoxButtons.OK); } else { //get collectionid number DataGridCell myCell = new DataGridCell(); myCell.RowNumber = dgNew.CurrentRowIndex; myCell.ColumnNumber = 4; Main.Req.strReq = Convert.ToString(dgNew[myCell]); //open form showing the request // Create a new instance of the child form. frmShowReq childForm = new frmShowReq(); // Make it a child of this MDI form before showing it. childForm.MdiParent = this.MdiParent; childForm.Text = "IO Codes"; childForm.Show(); } }
Code:private void btnUpdate_Click(object sender, EventArgs e) { //CHeck to see if there are any items if (txtDocID.Text.Length < 0) { MessageBox.Show("There is no print job selected to update.", "Error!"); } else { try { //Update the record to Approved myConnection = new SqlConnection("workstation id=S-KG;packet size=4096;integrated security=SSPI;data source=SCE;persist security info=False;initial catalog=ELibrary"); string cmdtext = "Update PrintJobs SET Number = @Number, Method = @Method, DueDate = @DueDate, CoverColor = @CoverColor, Color = @Color, Instructions = @Instructions, Name = @Name, Phone = @Phone, Contact = @Contact, IO = @IO, Status = @Status where PrintID = '" + id + "'"; SqlCommand sqlcmd; sqlcmd = new SqlCommand(cmdtext, myConnection); sqlcmd.Parameters.AddWithValue("@Number", cboNumber.Text); sqlcmd.Parameters.AddWithValue("@Method", cboMethod.Text); sqlcmd.Parameters.AddWithValue("@DueDate", dtDue.Text); sqlcmd.Parameters.AddWithValue("@CoverColor", cboCoverColor.Text); sqlcmd.Parameters.AddWithValue("@Color", cboColor.Text); sqlcmd.Parameters.AddWithValue("@Instructions", txtInstructions.Text); sqlcmd.Parameters.AddWithValue("@Name", txtName.Text); sqlcmd.Parameters.AddWithValue("@Phone", txtPhone.Text); sqlcmd.Parameters.AddWithValue("@Contact", chkContact.Checked.ToString()); sqlcmd.Parameters.AddWithValue("@IO", cboIO.Text); sqlcmd.Parameters.AddWithValue("@Status", cboStatus.Text); myConnection.Open(); sqlcmd.ExecuteNonQuery(); MessageBox.Show("Print job has been successfully changed.", "Print job changed", MessageBoxButtons.OK); myConnection.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error!"); myConnection.Close(); } } }




Reply With Quote