Data Gridview不重新加载更新的数据

时间:2016-11-03 07:45:29

标签: c# winforms 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  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);

            }
        }
    }

我的代码有问题吗?我想不是因为数据被插入并更新到数据库中。请帮帮我

1 个答案:

答案 0 :(得分:0)

您需要使用数据填充网格视图。我认为你应该在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();
            }
        }
    }