C# 单击“更新”按钮时,Datagrid视图未更新
实际上,当我点击datagrid视图的行或单元格时,它们会填充到文本框中进行编辑,在我编辑并点击更新后,datagridview不会立即改变,如果我关闭并再次运行表单,它会改变。我的要求是它应该在我点击更新按钮后立即更改。我用于“更新”单击的代码是:C# 单击“更新”按钮时,Datagrid视图未更新,c#,winforms,datagridview,C#,Winforms,Datagridview,实际上,当我点击datagrid视图的行或单元格时,它们会填充到文本框中进行编辑,在我编辑并点击更新后,datagridview不会立即改变,如果我关闭并再次运行表单,它会改变。我的要求是它应该在我点击更新按钮后立即更改。我用于“更新”单击的代码是: private void btnUpdate_Click(object sender, EventArgs e) { SqlConnection con = Helper.getconnection(); Sql
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();
}
请告诉我我犯了什么错误。好吧,我想说的是,从一开始,你就把事情按错误的顺序处理了。。。你应该在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();
}
}
protectedvoidbtnupdate\u单击(对象发送方,事件参数e)
{
//首先更新数据库
更新项目详细信息(txtPrjID.Text、txtPrjNmae.Text、txtPrjdescription.Text、txtPrjDate.Text、txtPrjSize.Text、txtPrjManager.Text);
//从数据库中获取新结果
IEnumerable projectDetails=GetProjectDetails();
//更新用户界面
dataGridView2.DataSource=项目详细信息;
dataGridView2.Update();
dataGridView2.Refresh();
//提醒用户
MessageBox.Show(“项目详细信息已更新”);
}
public void UpdateProjectDetails(字符串prjID、字符串prjName字符串描述、字符串日期、字符串大小、字符串管理器)
{
使用(DbConnection con=Helper.getconnection())
{
con.Open();
DbCommand cmd=con.CreateCommand();
cmd.CommandType=CommandType.Text;
cmd.CommandText=“更新项目集项目名=”+PrjName+”,描述=“+Description+”,日期开始=“+Date+”,团队规模=“+Size+”,经理=“+Manager+”,其中projectd=“+PrjID+”;
cmd.Connection=con;
cmd.ExecuteNonQuery();
}
}
要记住两件重要的事情:
首先,在使用DB或Streams时,您应该使用using
语句,或者尝试catch finally并关闭finally
块中的连接
其次,如果要在DB更新之后更新dataGridView
,则应在更新之后执行。在您的代码中,您是在更新之前执行的
第三,也是最重要的您的DB命令是危险的,并且对SQL注入开放。使用参数化命令几乎总是更好:
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
,但可能需要它再次从数据库中获取数据。但是,本部分仅包含调用程序开始时执行的相同命令的步骤在执行查询之前更新视图。并不是说仅更新它就有任何好处——如果数据来自数据库,则需要在执行查询后再次获取数据,然后更新/刷新视图。此外,内联查询总是一个可怕的想法。@S\u F你是说我应该使用DataBind()吗方法,因为数据来自数据库?您没有在代码中设置datasource属性。@NoIdeaForName这不是问题的一部分:)用户执行了类似于打开sql注入查询的操作,但没有被询问,这并不意味着您也应该忽略它。@NoIdeaForName在这种情况下,为什么不建议使用存储过程而不是动态SQL或实现EntityFramework或NHibernate而不是。。。
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();