C# Data Gridview未重新加载更新的数据

C# Data Gridview未重新加载更新的数据,c#,winforms,gridview,C#,Winforms,Gridview,我想在gridview中重新加载更新的记录,但它不起作用。网格视图在插入任何新记录后重新加载,但在更新记录后不重新加载。尽管记录保存在数据库中,并且在我重新启动应用程序时,gridview会加载更新的记录。我不知道为什么在我更新时它不是重新加载数据,而是在我插入新记录时重新加载数据,尽管我调用的函数与我在插入中调用的函数相同 这里是插入代码 private void InsertEmployee() { if (tbName.Text != "" &am

我想在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()方法中添加绑定:


您需要用数据填充网格视图。我认为应该在FillGridView()方法中添加绑定:



我找不到将DataTable绑定到GridView的代码。它应该是这样的:gridView1.DataSource=dt;在FillGridView中method@mojoc#winforms应用程序中没有绑定。我想您应该将网格数据源设置为dt?好的。您的GridView中填充了数据。你们的方法FillGridView应该被称为FillDataTable。是的,我已经设置好了,但我想我在发布问题时错过了那个部分。。仍然不工作我找不到将DataTable绑定到GridView的代码。它应该是这样的:gridView1.DataSource=dt;在FillGridView中method@mojoc#winforms应用程序中没有绑定。我想您应该将网格数据源设置为dt?好的。您的GridView中填充了数据。你们的方法FillGridView应该被称为FillDataTable。是的,我已经设置好了,但我想我在发布问题时错过了那个部分。。仍然不工作尝试添加:gridView.Update();UpdateEmployee方法调用button click事件吗?是的,调用了MethodOne注释,您不应该使用con.Close(),因为连接已经在using语句中。根据GridView,我现在不知道为什么它没有更新。我认为应该在FillGridView方法中打开连接;UpdateEmployee方法调用button click事件吗?是的,调用了MethodOne注释,您不应该使用con.Close(),因为连接已经在using语句中。根据GridView,我现在不知道为什么它没有更新。我认为应该在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();
            }
        }
    }