How to update or show immediately in datagridview after insertion?

After entering data into the entire text field and after clicking the submit button, it will not immediately appear in the datagridview, I need to open the form again to see the new inserted row. What code to include for updating?

The following code is @ user3222297. by adding grdPatient.Update (); and grdPatient.Refresh (); still not updating after I click OK to insert successfully.

doesn't get refresh

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using System.Configuration; namespace GRP_02_03_SACP { public partial class patient : Form { // Data Table to store employee data DataTable Patient = new DataTable(); // Keeps track of which row in Gridview // is selected DataGridViewRow currentRow = null; SqlDataAdapter PatientAdapter; public patient() { InitializeComponent(); } private void btnSubmit_Click(object sender, EventArgs e) { if (btnSubmit.Text == "Clear") { btnSubmit.Text = "Submit"; txtpFirstName.Focus(); } else { btnSubmit.Text = "Clear"; int result = AddPatientRecord(); if (result > 0) { MessageBox.Show("Insert Successful"); grdPatient.Update(); grdPatient.Refresh(); } else MessageBox.Show("Insert Fail"); } } private int AddPatientRecord() { int result = 0; // TO DO: Codes to insert customer record //retrieve connection information info from App.config string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnection"].ConnectionString; //STEP 1: Create connection SqlConnection myConnect = new SqlConnection(strConnectionString); //STEP 2: Create command String strCommandText = "INSERT PATIENT(pFirstName, pLastName, pContact, pAddress, pCity, pZip, pNationality, pRace, pIC, pGender, pDOB, pBloodType, pEmail) " + " VALUES (@pFirstName,@pLastName,@pContact,@pAddress,@pCity,@pZip,@pNationality, @pRace, @pIC, @pGender, @pDOB, @pBloodType, @pEmail)"; SqlCommand updateCmd = new SqlCommand(strCommandText, myConnect); updateCmd.Parameters.AddWithValue("@pFirstName", txtpFirstName.Text); updateCmd.Parameters.AddWithValue("@pLastName", txtpLastName.Text); //updateCmd.Parameters["@clientid"].Direction = ParameterDirection.Output; updateCmd.Parameters.AddWithValue("@pContact", txtpContact.Text); updateCmd.Parameters.AddWithValue("@pAddress", txtpAddress.Text); updateCmd.Parameters.AddWithValue("@pCity", txtpCity.Text); updateCmd.Parameters.AddWithValue("@pZip", txtpZip.Text); updateCmd.Parameters.AddWithValue("@pNationality", txtpNationality.Text); updateCmd.Parameters.AddWithValue("@pRace", txtpRace.Text); updateCmd.Parameters.AddWithValue("@pIC", txtpIC.Text); updateCmd.Parameters.AddWithValue("@pGender", txtpGender.Text); updateCmd.Parameters.AddWithValue("@pDOB", txtpDOB.Text); updateCmd.Parameters.AddWithValue("@pBloodType", txtpBloodType.Text); updateCmd.Parameters.AddWithValue("@pEmail", txtpEmail.Text); // STEP 3 open connection and retrieve data by calling ExecuteReader myConnect.Open(); // STEP 4: execute command // indicates number of record updated. result = updateCmd.ExecuteNonQuery(); // STEP 5: Close myConnect.Close(); return result; } private void patient_Load(object sender, EventArgs e) { LoadPatientRecords(); } private void LoadPatientRecords() { //retrieve connection information info from App.config string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnection"].ConnectionString; //STEP 1: Create connection SqlConnection myConnect = new SqlConnection(strConnectionString); //STEP 2: Create command string strCommandText = "SELECT pFirstName, pLastName, pContact, pAddress, pCity, pZip, pNationality, pRace, pIC, pGender, pDOB, pBloodType, pEmail, pUsername, pPassword FROM Patient"; PatientAdapter = new SqlDataAdapter(strCommandText, myConnect); //command builder generates Select, update, delete and insert SQL // statements for MedicalCentreAdapter SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(PatientAdapter); // Empty Employee Table first Patient.Clear(); // Fill Employee Table with data retrieved by data adapter // using SELECT statement PatientAdapter.Fill(Patient); // if there are records, bind to Grid view & display if (Patient.Rows.Count > 0) grdPatient.DataSource = Patient; } } } 
+6
c # insert refresh button datagridview
Jan 23 '14 at 3:53
source share
6 answers

Use LoadPatientRecords() after successful installation.

Try the code below

 private void btnSubmit_Click(object sender, EventArgs e) { if (btnSubmit.Text == "Clear") { btnSubmit.Text = "Submit"; txtpFirstName.Focus(); } else { btnSubmit.Text = "Clear"; int result = AddPatientRecord(); if (result > 0) { MessageBox.Show("Insert Successful"); LoadPatientRecords(); } else MessageBox.Show("Insert Fail"); } } 
+2
Jan 27 '14 at 7:38
source share

You need to populate the datagrid again as follows:

 this.XXXTableAdapter.Fill(this.DataSet.XXX); 

If you use automaticlly connect from dataGridView, this code automatically creates in Form_Load ()

+6
May 11 '15 at 18:21
source share

You can set datagridview DataSource to null and retype it again.

 private void button1_Click(object sender, EventArgs e) { myAccesscon.ConnectionString = connectionString; dataGridView.DataSource = null; dataGridView.Update(); dataGridView.Refresh(); OleDbCommand cmd = new OleDbCommand(sql, myAccesscon); myAccesscon.Open(); cmd.CommandType = CommandType.Text; OleDbDataAdapter da = new OleDbDataAdapter(cmd); DataTable bookings = new DataTable(); da.Fill(bookings); dataGridView.DataSource = bookings; myAccesscon.Close(); } 
+3
Apr 05 '15 at 1:59
source share

Try updating datagrid after each insert

 datagridview1.update(); datagridview1.refresh(); 

Hope this helps you!

+1
Jan 23 '14 at 5:36 on
source share

I don’t know if you solved your problem, but an easy way to resolve this is to restore the DataSource (this property) of your datagridview. For example:

 grdPatient.DataSource = MethodThatReturnList(); 

So, in this MethodThatReturnList () method, you can create a list (a list is a class) with all the necessary elements. In my case, I have a method that returns the values ​​for the two columns that I have on my datagridview.

Pasha.

0
Dec 03
source share

In the form designer, add a new timer using the toolbar. The properties are set to Enabled to True.

enter image description here

Set DataGridView to equal your new timer data

enter image description here

0
Nov 06 '17 at 18:19
source share



All Articles