开发者

error when edit row in datagrid view

I have a DataGridView with an edit option. I did my code, but this error appeared:

Failed to convert parameter value from a DataGridViewTextBoxCell to a Int32

Here is my code:

private void UpdateRecored(int rowindex)
{
 int rowaffected = 0;
 DataGridViewCell Number = (DataGridViewCell)DGCategory.Rows[rowindex].Cells[1];
 DataGridViewCell Category = (DataGridViewCell)DGCategory.Rows[rowindex].Cells[2];
 DataGridViewCell Parent = (DataGridViewCell)DGCategory.Rows[rowindex].Cells[3];
 DataGridViewCell Active = (DataGridViewCell)DGCategory.Rows[rowindex].Cells[4];
 DataGridViewCell AddDate = (DataGridViewCell)DGCategory.Rows[rowindex].Cells[5];
 using (SqlConnection Con = GetConnection())
 {
   SqlCommand cmd = new SqlCommand("SP_UpdateCategory", Con);
   cmd.CommandType = CommandType.StoredProcedure;
   cmd.Parameters.Add("@Id", SqlDbType.Int).Value = Number;
   cmd.Parameters.Add("@Category", SqlDbType.NVarChar).Value = Category;
   cmd.Parameters.Add("@ParentID", SqlDbType.Int).Value = Parent;
   cmd.Parameters.Add("@Active", SqlDbType.Bit).Value = Active;
   cmd.Parameters.Add("@AddDate", SqlDbType.DateTime).Value = AddDate;
   rowaffected = cmd.ExecuteNonQuery();
   if (rowaffected > 0)
   {
      M开发者_运维百科essageBox.Show("Row Updated");
   }
   }
   }

   private void DGCategory_CellClick(object sender, DataGridViewCellEventArgs e)
    {
      if (DGCategory.Columns[e.ColumnIndex].Name == "Edit")
     {
            UpdateRecored(Convert.ToInt32(e.RowIndex));
      }
    }


DataGridViewCell represents the cell of the datagridview control not the value in it (i.e Number.Value). If your cell has a textbox in it you need to get to value from the textbox.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜