I got my form "InsertClient" and I have the button click method
public void Insert_Button_Click(object sender, EventArgs e)
{
MyClass.InsertNewClient(fullNametxt.Text, shortNametxt.Text);
fullNametxt.Clear();
shortNametxt.Clear();
fullNametxt.Focus();
GridView.Update();
GridView.Refresh();
}
My class recieve this:
public static void InsertNewClient (String fullName, String shortName)
{
SqlConnection conn = DBClass.ConnectionString.GetConnection();
SqlCommand cmd = new SqlCommand("MyStoredProcedure", conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@fullName", fullName);
cmd.Parameters.AddWithValue("@shortName", shortName);
int i = cmd.ExecuteNonQuery();
if (i == 0)
{
MessageBox.Show("Can't save data");
}
if (i > 0)
{
MessageBox.Show("Data saved!");
}
}
The thing is, the data is saved but that the DataGridView does not refresh after each INSERT (button click). I have to close the form and re-open it and appears refreshed.
Please help, I want the DataGridView refresh after INSERT data
cCUSTOMERBindingSource
Is the object of my BindingSource generated using ToolBox.
public void ReloadGrid()
{
Cursor.Current = Cursors.WaitCursor;
cCUSTOMERBindingSource.DataSource = bd.C_CUSTOMER.ToList();
Cursor.Current = Cursors.Default;
}
this where I called the method
private void bunifuThinButton21_Click(object sender, EventArgs e)
{
C_CUSTOMER cst = new C_CUSTOMER();
C_ACCOUNT acc = new C_ACCOUNT();
cst.FIRST_NAME = txtFname.Text;
cst.MIDDLE_NAME = txtMname.Text;
cst.LAST_NAME = txtLname.Text;
cst.LOCATION = txtlocation.Text;
cst.DATE_OF_BIRTH = Convert.ToDateTime(dateTimePicker1.Text);
acc.ACCOUNT_BALANCE = Convert.ToInt32(txtInitAmoun.Text);
acc.ACCOUNT_NUMBER = Convert.ToInt32(txtAccNumber.Text);
cst.TELEPHONE = Convert.ToInt32(txtTelephone.Text);
cst.DATE_CREATE = DateTime.Now;
int newID = cstDao.insertCustomer(cst.FIRST_NAME, cst.MIDDLE_NAME, cst.LAST_NAME, cst.LOCATION, cst.DATE_OF_BIRTH, cst.TELEPHONE, cst.MODIFY_BY, cst.DATE_MODIFY, cst.DATE_CREATE);
acc.CUSTOMER_ID = newID;
acc.DATE_CREATED = DateTime.Now;
acc.CREATED_BY = 1;
int newAccID = cstDao.insertAccount(acc);
if(newID != 0 && newAccID != 0) {
MessageBox.Show("Insert Succefull", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Error during the insertion", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
ReloadGrid();
}
On Form load
private void Form3_Load(object sender, EventArgs e)
{
bd = new Customer_DBEntities();
cCUSTOMERBindingSource.DataSource = bd.C_CUSTOMER.ToList();
}
您应该有单独的方法,例如Refresh(),在这种情况下,您将使用sqlDataReader从sql获取数据,并在insertBtn调用该方法并将dataGridView数据源初始化后使用该方法
Something like that @mmking, but not exactly I already solved it.. the problem was exactly what @Fabio said...
Inside of the button click method I fill again de DataGridView with the DataSet.
public void Insert_Button_Click(object sender, EventArgs e)
{
MyClass.InsertNewClient(fullNametxt.Text, shortNametxt.Text);
this.tblClientTableAdapter.Fill(this.DataSetClient.tblClient);
}
Just to get clear... I use the DataGridView of toolbox, select "Choose data source" and select the table that I want to use to fill DataGrid...
I use the DataSetName that gave me by default and put it right like I show it in the code
Hope it helps for future questions
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.