1
ちょうど明確にしたいのは、挿入または更新または削除した後にdatagridviewをリフレッシュできますか? 私はそれをgoogled、それを行うにはまだ考えている。ここ新しいSQLクエリを選択せずに挿入または更新または削除した後にデータグリッドビューを更新する
は私のコードは、データをロードすることで、button 5
挿入データは、データをロード
private void button4_Click(object sender, EventArgs e)
{
employee();
}
public void employee()
{
DataTable dtclubroom = new DataTable();
SqlCommand command = new SqlCommand();
SqlDataAdapter adapter = new SqlDataAdapter(command.CommandText, myConnection);
try
{
myConnection.Open();
dtclubroom.Clear();
command.Connection = myConnection;
command.CommandText = "Select * from employee ";
adapter.SelectCommand = command;
adapter.Fill(dtclubroom);
dataGridView2.DataSource = dtclubroom;
}
catch (Exception ex)
{
MessageBox.Show("error" + ex);
}
finally
{
myConnection.Close();
}
}
private void button5_Click(object sender, EventArgs e)
{
SqlCommand command2 = new SqlCommand();
try
{
myConnection.Open();
command2.CommandText = "insert into employee (name,id) values (@name,@id)";
command2.Connection = myConnection;
command2.Parameters.AddWithValue("@name","Leon");
command2.Parameters.AddWithValue("@id", "002");
command2.ExecuteNonQuery();
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
finally
{
myConnection.Close();
}
employee() //<- refresh datagridview
}
Button 4
です。もう一度employee()
メソッドを呼び出すことなく、datagridviewをリフレッシュする方法はありますか?
どのように、私はグローバルとグローバルとしてsqladapterとしてdatatableを宣言するまで、まだ動作しません。データテーブルは複製されます。とにかく、何らかの形で、挿入されるデータがたくさんあるので、グローバルとして宣言しています。 – chopperfield
@chopperfield - 私は自分の答えを更新しました。これを試してください。グローバル 'sqladapter'は必要ありません。いろいろなデータのために多くのコードを書く必要はありません。一度適切な場所で一般化された方法で実行し、どこからでも使用してください。 –
私はそれを試して、動作していません。ロジックでは、まだ選択可能なもの(旧)を使用している。もちろん挿入後には更新されません。 – chopperfield