所以我有这个DataGridView
其上有两列,我从我的SQL Server
数据库检索。现在,在第二列中,我们有一个在Windows应用程序设计器中显示为CheckBox的位域。因此,我想在CellContentClick
事件中能够更新刚刚取消选中的值到我的数据库中。但似乎我无处可去。更新DataGridView检查记录到数据库
这里是我下面的代码:
private void gvTurnOffNotifications_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
foreach (DataGridViewRow row in gvTurnOffNotifications.Rows)
{
DataGridViewCheckBoxCell cell = row.Cells[1] as DataGridViewCheckBoxCell;
//We don't want a null exception!
if (cell.Value != null)
{
bool result = Convert.ToBoolean(row.Cells[1].Value);
if (result == true)
{
//It's checked!
btnUpdateTurnOff.Enabled = true;
myConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (mySQLConnection = new SqlConnection(myConnectionString))
{
int temp = 1;
bool change = false;
string procedureName = "update UsersNotified Set AllowNotification='" + change + "' where AllowNotification='" + false+ "'";
mySQLCommand = new SqlCommand(procedureName, mySQLConnection);
mySQLCommand.CommandType = CommandType.Text;
mySQLCommand.Connection = mySQLConnection;
mySQLCommand.Connection.Open();
mySQLCommand.ExecuteNonQuery();
}
}
}
}
}
,然后当我点击我的“更新”按钮,我想发送的更新的GridData详情如下,在我的数据库存储:
private void btnUpdateTurnOff_Click(object sender, EventArgs e)
{
myConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (mySQLConnection = new SqlConnection(myConnectionString))
{
mySQLDataAdapter = new SqlDataAdapter("spGetAllUpdatedNotifications", mySQLConnection);
mySQLDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
mySQLCommand.Connection = mySQLConnection;
mySQLCommand.Connection.Open();
DataSet ds = new DataSet();
mySQLDataAdapter.Fill(ds);
mySQLDataAdapter.UpdateCommand = mySQLCommand;
mySQLDataAdapter.Update(ds);
}
}
我的Update块中的spGetAllUpdatedNotifications
对象是一个存储过程,我打电话只是为了从数据库中检索记录,所以我可以在我的DataSet
中即时更新它们。这里是下面的定义:
create proc spGetAllUpdatedNotifications
as
begin
SELECT UserName, AllowNotification FROM UsersNotified where AllowNotification=1
end
GO
欲了解更多背景:当我的窗体加载时,我从有他们AllowNotification字段设置为第1位(真在C#)数据库,一旦用户unticks选择的所有记录一个特定的用户(换句话说,该用户不会再被允许接收通知),一旦我点击更新按钮,它应该将该属性设置为false(数据库中的位0)。
而不是更新我已取消选中的一条记录,它更新所有这些记录。在这种情况下,“全部”是具有AllowNotification=1
的记录。我只想设置AllowNotification=0
为取消选中/未选中的记录只有
有关如何实现此目标的任何建议?
你目前的代码面临什么问题? –
而不是更新我已取消选中的一条记录,它会更新所有这些记录。在这种情况下,“全部”是具有'AllowNotification = 1'的记录。我只想为取消选中/未选中的记录设置AllowNotification = 0'。 –
因为在更新查询中没有where子句。 –