我想重新加载gridview中的更新记录,但它不工作。网格视图重新加载后,我插入任何新记录但不更新记录后重新加载。虽然记录保存在数据库中,当我重新启动应用程序时,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 [email protected]_name,[email protected]_fathername,[email protected]_nic,[email protected]_gender,[email protected]_contact,[email protected]_dob,[email protected]_bloodgroup,[email protected]_maritialstatus,[email protected]_address,[email protected]_per_address,[email protected]_picture WHERE [email protected]_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);
}
}
}
的方法有什么错我的代码?我想不是因为数据被插入并更新到数据库中。请帮助我
我无法找到将DataTable绑定到GridView的代码。它应该像这样:gridView1.DataSource = dt;在FillGridView方法 – mojo
@mojo没有绑定在C#winforms应用 –
我想你应该设置你的网格数据源为** DT **? – Badiparmagi