点击更新按钮时,数据网格视图未更新。

4

实际上,当我单击datagrid视图的行或单元格时,它们会填充到文本框中进行编辑,在我编辑并单击更新后,datagridview不会立即更改,如果我关闭并再次运行表单,则会更改。我的要求是在我点击更新按钮后应立即更改。 我用于更新单击的代码是:

 private void btnUpdate_Click(object sender, EventArgs e)
 {
        SqlConnection con = Helper.getconnection();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        string PrjID = txtPrjID.Text;
        string PrjName = txtPrjNmae.Text;
        string Description = txtPrjdescription.Text;
        string Date = txtPrjDate.Text;
        string Size = txtPrjSize.Text;
        string Manager = txtPrjManager.Text;
        cmd.CommandText = "Update Projects set ProjectName= '" + PrjName + "', Description='" + Description + "', DateStarted='" + Date + "',TeamSize='" + Size + "',Manager='" + Manager + "' where ProjectID= " + PrjID + " ";
        MessageBox.Show("Project Details are updated");
        dataGridView2.Update();
        dataGridView2.Refresh();
        con.Open(); 
        cmd.ExecuteNonQuery();
        con.Close();
  }

请告诉我我在做什么错误。


在执行查询之前,您正在更新视图。请注意,仅更新它是没有任何好处的 - 如果数据来自数据库,则需要在执行查询后再次获取它,然后更新/刷新视图。此外,内联查询总是一个可怕的想法。 - S_F
@S_F,您是在说我应该使用DataBind()方法,因为数据来自数据库吗? - user2749421
您的代码中未设置数据源属性。 - Avi Turner
3个回答

3

需要记住的两个重要事情:

首先,在使用数据库或流时,您应该使用using语句或try catch finally,并在finally块中关闭连接。

其次,如果您想在DB更新后更新dataGridView,则应该在更新之后进行。在您的代码中,您是在更新之前执行的。

第三个也是最重要的,您的DB命令存在危险并且容易受到SQL Injection攻击。使用参数化命令几乎总是更好的选择:

private void btnUpdate_Click(object sender, EventArgs e)
{
    UpdateDB();
    dataGridView2.Update();
    dataGridView2.Refresh();
}

private void UpdateDB()
{
    using (DbConnection con = Helper.getconnection())
    {
        con.Open();

        using(DbCommand cmd = con.CreateCommand("Update Projects set ProjectName= @PrjName, Description=@Description, DateStarted=@Date, TeamSize=@Size,Manager=@Manager where ProjectID=@PrjID "))
        {

           cmd.CommandType = CommandType.Text;
           cmd.Parameters.Add(new SqlParameter("PrjName", txtPrjNmae.Text));
           cmd.Parameters.Add(new SqlParameter("Description", txtPrjdescription.Text));
           cmd.Parameters.Add(new SqlParameter("Date", txtPrjDate.Text));
           cmd.Parameters.Add(new SqlParameter("Size", txtPrjSize.Text));
           cmd.Parameters.Add(new SqlParameter("Manager", txtPrjManager.Text));
           cmd.Parameters.Add(new SqlParameter("PrjID", txtPrjID.Text));
           cmd.Connection = con;

           cmd.ExecuteNonQuery();
        }
    }
}

现在我不知道您是如何将数据绑定到dataGridView的,但可能需要重新从数据库获取数据。此部分只需调用程序开始时使用的相同命令即可。


2

首先,我想说你的调用顺序有误...而且你应该在using语句内部建立连接。

protected void btnUpdate_Click(object sender, EventArgs e)
{
    // Update DB first
    UpdateProjectDetails(txtPrjID.Text, txtPrjNmae.Text, txtPrjdescription.Text, txtPrjDate.Text, txtPrjSize.Text, txtPrjManager.Text);

    // Fetch new results from DB
    IEnumerable<ProjectDetail> projectDetails = GetProjectDetails();

    // Update UI
    dataGridView2.DataSource = projectDetails;
    dataGridView2.Update();
    dataGridView2.Refresh();

    // Alert the user
    MessageBox.Show("Project Details are updated");
}

public void UpdateProjectDetails(string prjID, string prjName string description, string date, string size, string manager)
{
    using (DbConnection con = Helper.getconnection())
    {
        con.Open();

        DbCommand cmd = con.CreateCommand();

        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "Update Projects set ProjectName= '" + PrjName + "', Description='" + Description + "', DateStarted='" + Date + "',TeamSize='" + Size + "',Manager='" + Manager + "' where ProjectID= " + PrjID + " ";
        cmd.Connection = con;

        cmd.ExecuteNonQuery();
    }
}

@NoIdeaForName 这不是问题的一部分 :) - Squirrel5853
用户做了一些像打开他的查询以进行 SQL 注入的事情,但没有询问,这并不意味着你也应该忽视它。 - No Idea For Name
1
@NoIdeaForName 如果这样的话,为什么不建议使用存储过程代替动态SQL或者实现EntityFramework或NHibernate呢... - Squirrel5853

1
SqlConnection con = Helper.getconnection();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            con.Open();
            cmd.CommandType = CommandType.Text;
            string PrjName = txtPrjNmae.Text;
            string Description = txtPrjdescription.Text;
           string Date = txtPrjDate.Text;
            string Size = txtPrjSize.Text;
            string Manager = txtPrjManager.Text;
            cmd.CommandText = "Update Projects set Description='" + Description + "', DateStarted='" + Date + "',TeamSize='" + Size + "',Manager='" + Manager + "' where ProjectName= '" + PrjName + "' ";
            MessageBox.Show("Project Details are updated");
            dataGridView2.Update();
            dataGridView2.Refresh();
            cmd.ExecuteNonQuery();
            con.Close();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接