大家好我有一個DataGridView,裏面是2列,FullName和VoteCount。我想刷新DataGridView,以查看數據庫中的更改。如何在不關閉表單或單擊任何按鈕的情況下刷新DatagridView?可能嗎?如何刷新新存儲的數據的datagridview?
這裏是我的代碼:
private void President()
{
sc.Open();
cmd = new SqlCommand("SELECT (LastName + ', ' + FirstName + ' ' + MiddleName) as FullName,Vcount as VoteCount FROM TableVote WHERE Position='President'", sc);
try
{
_da = new SqlDataAdapter();
_da.SelectCommand = cmd;
DataTable _dt = new DataTable();
_da.Fill(_dt);
BindingSource bs = new BindingSource();
bs.DataSource = _dt;
PresDG.DataSource = bs;
_da.Update(_dt);
PresDG.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
PresDG.Columns["FullName"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
PresDG.Columns["VoteCount"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
PresDG.Columns["VoteCount"].Width = (100);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sc.Close();
}
}
你可以在另一個線程中做到這一點,我猜測每隔幾分鐘左右就會觸發一些方法。 – Tafari