我想將我的Datagridview綁定到SQL數據庫。當我從另一個源(從Visual Stuido中的SQL查詢)更新數據庫時,更改不會顯示在DataGridView或基礎DataTable中。DataTable附加到SQL數據庫與查詢功能不會自動更新
我正在運行我的程序,實際上使用下面的函數填充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 – 2014-09-26 04:15:19
不確定你的意思,DGV綁定到表後的BindingSource。 – janderson 2014-09-26 04:19:36
感謝您的幫助,但是對於windows窗體datagridview沒有DataBind()方法:http://social.msdn.microsoft.com/forums/windows/en-US/2639235d-82ff-4946-ba95-3c0ee117544d/why -there-is-no-databind-method-for-datagridview – janderson 2014-09-26 04:42:33