Hi everyone I have a DataGridView, inside of it is 2 columns, FullName and VoteCount. I want to refresh the DataGridView, for changes in my Database. How do I refresh the DatagridView without closing the form or clicking any button? Is it possible?
Here's my code :
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();
}
}