2017-06-28 36 views
0

所以我有这个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为取消选中/未选中的记录只有

有关如何实现此目标的任何建议?

+0

你目前的代码面临什么问题? –

+0

而不是更新我已取消选中的一条记录,它会更新所有这些记录。在这种情况下,“全部”是具有'AllowNotification = 1'的记录。我只想为取消选中/未选中的记录设置AllowNotification = 0'。 –

+0

因为在更新查询中没有where子句。 –

回答

1

我不确定什么逻辑让你循环通过DataGridView的所有行只是为了更新数据库中的一行。

如果您想要更新AllowNotification值的复选框被选中或取消选中的用户名,逻辑就是这样。

  1. 找出在gridview中点击的复选框的更新值。
  2. 将更新的值(True或False)存储在布尔变量中。
  3. 从gridview的同一行的另一个单元格中检索相应的用户名。
  4. 使用标准“WHERE UserName = {userName}”执行更新查询。

你需要写在DataGridView的CellContentClick事件如下。

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 
{ 
    if (e.ColumnIndex == 1) //Assuming Checkbox is displayed in 2nd column. 
    { 
     this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit); 

     var result = this.dataGridView1[e.ColumnIndex, e.RowIndex].Value; 
     var userName = this.dataGridView1[0, e.RowIndex].Value; //Assumin username is displayed in fist column 

     var connectionString = "Your Connection String"; 
     //Set value of your own connection string above. 

     var sqlQuery = "UPDATE UsersNotified SET AllowNotification = @allowNotification WHERE UserName = @userName"; 

     using (var connection = new SqlConnection(connectionString)) 
     { 
      using (var command = new SqlCommand(sqlQuery, connection)) 
      { 
       command.CommandType = CommandType.Text; 
       command.Parameters.Add("@allowNotification", SqlDbType.Bit).Value = result; 
       command.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = userName; 
       connection.Open(); 
       command.ExecuteNonQuery(); 
      } 
     } 
    } 
} 

这应该有助于您解决问题。

+0

谢谢,完美的作品! –

0

我有一个部分溶液(它不工作了100%,但至少它在正确的方向迈出的一步):

private void gvTurnOffNotifications_SelectionChanged(object sender, EventArgs e) 
     { 
      if (gvTurnOffNotifications.SelectedCells.Count > 0) 
      { 
       int selectedrowindex = gvTurnOffNotifications.SelectedCells[0].RowIndex; 

       DataGridViewRow selectedRow = gvTurnOffNotifications.Rows[selectedrowindex]; 

       getUserSelected = Convert.ToString(selectedRow.Cells["UserName"].Value); 

       MessageBox.Show(getUserSelected); 

       foreach (DataGridViewRow row in gvTurnOffNotifications.Rows) 
       { 
        DataGridViewCheckBoxCell cell = row.Cells[1] as DataGridViewCheckBoxCell; 

        //We don't want a null exception! 
        if (cell.Value != null) 
        { 
         //It's checked! 
         btnUpdateTurnOff.Enabled = true; 
         myConnectionString = ConfigurationManager.ConnectionStrings["FSK_ServiceMonitor_Users_Management.Properties.Settings.FSK_ServiceMonitorConnectionString"].ConnectionString; 
         using (mySQLConnection = new SqlConnection(myConnectionString)) 
         { 
          bool change = false; 

          string procedureName = "update UsersNotified Set AllowNotification='" + change + "' where UserName='" + getUserSelected + "'"; 
          //MessageBox.Show(cell.Value.ToString()); 
          mySQLCommand = new SqlCommand(procedureName, mySQLConnection); 
          mySQLCommand.CommandType = CommandType.Text; 
          mySQLCommand.Connection = mySQLConnection; 
          mySQLCommand.Connection.Open(); 
          mySQLCommand.ExecuteNonQuery(); 
         } 
        } 
       } 
      } 
     } 

问题是,它只是采用第一行没有我选择了我想取消选择的行。