实际上,当我点击数据网格视图的行或单元格时,它们会填充到文本框中进行编辑,在我编辑和查看更新后,如果我关闭并运行数据网格,则数据网格视图不会立即更改再次形成,它正在改变。我的要求是我应该在点击更新按钮后立即更改。我用于更新点击的代码是:
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();
}
请告诉我我在做什么错误。
答案 0 :(得分:3)
要记住的两件重要事情:
首先使用数据库或流时,您应使用using
语句或尝试最后捕获并关闭finally
块中的连接。
第二,如果您想在数据库更新后更新dataGridView
,那么您应该在>更新后进行更新。在您的代码中,您在更新之前就已经这样做了。
第三,最重要您的数据库命令是危险的,并且对 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
的,但是可能需要再次从DataBase中获取数据。但是,这一部分只包含调用您在程序开头执行的相同命令
答案 1 :(得分:2)
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();
}
}
答案 2 :(得分: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();