数据网格视图没有重新加载更新的数据
本文关键字:数据 加载 更新 网格 数据网 视图 新加载 | 更新日期: 2023-09-27 18:16:17
我想在gridview中重新加载更新的记录,但它不起作用。网格视图在我插入任何新记录后重新加载,但在我更新记录后不重新加载。虽然记录保存在数据库中,当我重新启动应用程序时,gridview加载更新的记录。我不知道为什么当我更新时不重新加载数据,但当我插入新记录时重新加载数据,虽然我调用的函数与我在插入中调用的相同这里是插入代码
private void InsertEmployee()
{
if (tbName.Text != "" && mtbCNIC.Text != "" && cBoxBloodGroup.SelectedIndex != -1 && cBoxMaritialStatus.SelectedIndex != -1 && tbAddress.Text != "" && tbFatherName.Text != "" && cBoxGender.SelectedIndex != -1 && tbPerAddress.Text != "")
{
CS = ConfigurationManager.ConnectionStrings["HRMSConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT ISNULL(MAX(emp_id),0)+1 FROM EMP_Master", con);
cmd.CommandType = CommandType.Text;
tbID.Text = cmd.ExecuteScalar().ToString();
{
using (SqlCommand cmd1 = new SqlCommand("INSERT INTO EMP_Master (emp_id , emp_name,emp_fathername,emp_nic,emp_gender,emp_contact,emp_dob,emp_bloodgroup,emp_maritialstatus,emp_address,emp_per_address,emp_picture)VALUES(@emp_id , @emp_name,@emp_fathername,@emp_nic,@emp_gender,@emp_contact,@emp_dob,@emp_bloodgroup,@emp_maritialstatus,@emp_address,@emp_per_address,@emp_picture)", con))
{
//con.Open();
cmd1.CommandType = CommandType.Text;
cmd1.Parameters.AddWithValue("@emp_id", tbID.Text);
cmd1.Parameters.AddWithValue("@emp_name", tbName.Text);
cmd1.Parameters.AddWithValue("@emp_fathername", tbFatherName.Text);
cmd1.Parameters.AddWithValue("@emp_nic",mtbCNIC.Text);
//string tbMaskCNIC = mtbCNIC.Text;
//tbMaskCNIC = tbMaskCNIC.Replace("-","");
//cmd1.Parameters.AddWithValue("@emp_nic", int.Parse(tbMaskCNIC));
cmd1.Parameters.Add("@emp_gender", SqlDbType.VarChar, 50);
cmd1.Parameters["@emp_gender"].Value = cBoxGender.SelectedItem;
cmd1.Parameters.AddWithValue("@emp_contact", tbContact.Text);
cmd1.Parameters.AddWithValue("@emp_dob", dtpBirth.Value.Date);
cmd1.Parameters.AddWithValue("@emp_bloodgroup",cBoxBloodGroup.SelectedItem.ToString());
cmd1.Parameters.AddWithValue("@emp_maritialstatus",cBoxMaritialStatus.SelectedItem.ToString());
cmd1.Parameters.AddWithValue("@emp_address", tbAddress.Text);
cmd1.Parameters.AddWithValue("@emp_per_address", tbPerAddress.Text);
cmd1.Parameters.AddWithValue("@emp_picture", SaveImage());
cmd1.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Has been Saved Successfully !", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
FillGridView();
ResetForm();
tbName.Focus();
}
}
}
}
else
{
MessageBox.Show("All Fields are Mandatory !", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
tbName.Focus();
}
//catch (Exception)
//{
// MessageBox.Show("Something is wrong");
//}
}
,这里是更新代码
private void UpdateEmployee()
{
try
{
//if (tbName.Text != "" && mtbCNIC.Text != "" && tbContact.Text != "" && tbAddress.Text != "" && tbFatherName.Text != "" && cBoxBloodGroup.SelectedIndex != -1 && cBoxGender.SelectedIndex != -1 && tbPerAddress.Text != "" && dtpBirth.Text != "")
//{
CS = ConfigurationManager.ConnectionStrings["HRMSConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
{
using (SqlCommand cmd1 = new SqlCommand("UPDATE EMP_Master SET emp_name=@emp_name,emp_fathername=@emp_fathername,emp_nic=@emp_nic,emp_gender=@emp_gender,emp_contact=@emp_contact,emp_dob=@emp_dob,emp_bloodgroup=@emp_bloodgroup,emp_maritialstatus=@emp_maritialstatus,emp_address=@emp_address,emp_per_address=@emp_per_address,emp_picture=@emp_picture WHERE emp_id=@emp_id", con))
{
con.Open();
cmd1.CommandType = CommandType.Text;
cmd1.Parameters.AddWithValue("@emp_id", tbID.Text);
cmd1.Parameters.AddWithValue("@emp_name", tbName.Text);
cmd1.Parameters.AddWithValue("@emp_fathername", tbFatherName.Text);
cmd1.Parameters.AddWithValue("@emp_nic", mtbCNIC.Text);
cmd1.Parameters.Add("@emp_gender", SqlDbType.VarChar, 50);
cmd1.Parameters["@emp_gender"].Value = cBoxGender.SelectedItem;
cmd1.Parameters.AddWithValue("@emp_contact", tbContact.Text);
cmd1.Parameters.AddWithValue("@emp_dob", Convert.ToDateTime(dtpBirth.Value.ToString()));
cmd1.Parameters.AddWithValue("@emp_bloodgroup", cBoxBloodGroup.SelectedItem.ToString());
cmd1.Parameters.AddWithValue("@emp_maritialstatus", cBoxMaritialStatus.SelectedItem.ToString());
cmd1.Parameters.AddWithValue("@emp_address", tbAddress.Text);
cmd1.Parameters.AddWithValue("@emp_per_address", tbPerAddress.Text);
cmd1.Parameters.AddWithValue("@emp_picture", SaveImage());
cmd1.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Has been Updated Successfully !", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
FillGridView();
ResetForm();
tbName.Focus();
}
}
}
//}
//else
//{
// MessageBox.Show("All Fields are Mandatory !", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
// tbName.Focus();
//}
}
catch (Exception)
{
throw;
}
}
这是加载数据到gridview的方法
private void FillGridView()
{
CS = ConfigurationManager.ConnectionStrings["HRMSConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
using (SqlCommand cmd = new SqlCommand(@"SELECT * FROM EMP_Master", con))
{
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
ad.Fill(dt);
}
}
}
我的代码有问题吗?我猜不是,因为数据被插入和更新到数据库中。
您需要用数据填充网格视图。我认为你应该在FillGridView()方法中添加绑定:
private void FillGridView()
{
CS = ConfigurationManager.ConnectionStrings["HRMSConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
using (SqlCommand cmd = new SqlCommand(@"SELECT * FROM EMP_Master", con))
{
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
ad.Fill(dt);
gridView.DataSource = dt;
gridView.Update();
}
}
}