私はこの方法で達成することができました。この助けを願っています。 ブール値/ビットフィールドを持つdatagridviewを持っていて、datagridviewで行ったchecked/uncheckedイベントに基づいてデータベース内のフィールドを更新する場合は、このコードを書き込むことができます(Update Buttonのclickイベントで、ブールフィールド)
try
{
foreach (DataGridViewRow row in dataGridView1.Rows) //looping through each records in datagridview
{
//Update query
strQuery = @"Update TableA Set ActiveInactive = @ActiveInactive --Boolean field
Where Id = @Id ";
cmd = new SqlCommand(strQuery, // provide sql connection string here));
cmd.Parameters.AddWithValue("@Id", row.Cells["SystemIdentifier"].Value); //reading Id of a particular record
cmd.Parameters.AddWithValue("@ActiveInactive", SqlDbType.Bit).Value = (Convert.ToBoolean(row.Cells["ActiveInactive"].Value) ? 1 : 0);
//reading Boolean field of a particular record
cmd.ExecuteNonQuery();
}
MessageBox.Show("The records in the table has been updated.", "Information");
}
catch (Exception err)
{
MessageBox.Show(err.Message.ToString(), "Error");
}
注:私は、フォームのロードで必要な変数を宣言していたし、必要なときに呼び出されたSQL接続文字列のためのクラスを作成していました。
これは機能していないようです。 "dataGridView.items" - アイテムがエラーをスローします。ここに何かがないのですか? –
実際、このコードはTelerikの例です。役に立たない。私がDataGridViewに対して行った編集を試してみてください。 – Forklift