在 vb.net 中更新后刷新数据网格视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15231036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:37:25  来源:igfitidea点击:

refresh data grid view after updating in vb.net

sql-servervb.netvisual-studio-2010datagridview

提问by Ahla

i have a data grid view and details for a particular table.i update the details, But it doesn't get updated in the data grid view?my update statement works properly because, once i exit the project, it gets updated.

我有一个数据网格视图和特定表的详细信息。我更新了详细信息,但它没有在数据网格视图中更新?我的更新语句工作正常,因为一旦我退出项目,它就会更新。

cmd.CommandText = "update emp_tbl set sal= '" & SalTextBox.Text & "' where id='" & id & "'"

table name: emp_tbl

表名: emp_tbl

datagridview: emp_tbldatagridview

数据网格视图: emp_tbldatagridview

update button :

更新按钮:

cn.Open()

cmd.CommandText = "update emp_tbl set sal= '" & SalTextBox.Text & "' where id='" & id & "'"

cmd.ExecuteNonQuery()
cn.Close()

回答by RandomUs1r

You have to re-bind it:

您必须重新绑定它:

     BindingSource binding = new BindingSource(); //req. by win forms
     DataTable dt = new DataTable();
     dt.Load(sql_command.ExecuteReader());
     dgv.DataSource = dt;

This is the best way I've found to do it in win forms, .update doesn't work because it needs to actually re-pull the data from SQL.

这是我发现在 win 表单中执行此操作的最佳方法, .update 不起作用,因为它实际上需要从 SQL 重新提取数据。

回答by Kasnady

Just try to reuse your startup code in datagridview or recall the startup code from the data. It was the simple way at all. Cause your startup code is for binding your database to your datagridview. So, everytime you save, your code just save it and not work for rebind it again. So, what you need just try to rebind by recall the startup code

只需尝试在 datagridview 中重用您的启动代码或从数据中调用启动代码。这是最简单的方法。因为您的启动代码用于将您的数据库绑定到您的 datagridview。因此,每次保存时,您的代码只会保存它,而无法再次重新绑定。所以,你需要的只是尝试通过调用启动代码来重新绑定