こんにちは、私はDataGridViewを持っています。その中にはFullNameとVoteCountの 2 つの列があります。データベースの変更のために、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();
}
}