【发布时间】:2014-11-21 00:04:16
【问题描述】:
我正在尝试将我的 Datagridview 绑定到 SQL 数据库。当我从另一个源(从 Visual Stuido 中的 SQL 查询)更新数据库时,DataGridView 或基础 DataTable 中不会显示更改。
我正在运行我的程序,使用以下函数填充 DataGridView,然后使用 Visual Studio 中的 SQL 查询更改数据库。 DataGridView 不更新,DataTable 不触发 RowChanged 事件!
_query = "SELECT * FROM myFunction(0) ORDER BY timestamp DESC";
_connection = new SqlConnection("working connection string");
_adapter = new SqlDataAdapter(_query, _connection);
_cmdBuilder = new SqlCommandBuilder(_adapter);
_table = new DataTable();
_table.RowChanged += _table_RowChanged;
_adapter.Fill(_table);
_bindingSource = new BindingSource();
_bindingSource.DataSource = table;
_dataGridView.DataSource = _bindingSource;
// DataGridView is now populated
// run sql query externally from the program
// to update one of the rows/columns that the above function will return
// never updates datagridview and never updates the row in dataTable
我认为问题与功能有关,适配器是否不断通过查询轮询数据库?如果没有,我可以看到它无法判断它已更新。
【问题讨论】:
-
数据源后绑定gridview
-
不知道你的意思,DGV是绑定到表后的BindingSource的。
-
感谢您的帮助,但是windows窗体datagridview没有DataBind()方法:social.msdn.microsoft.com/forums/windows/en-US/…
标签: c# sql sql-server data-binding datagridview