[英]error when edit row in datagrid view
我有一個帶有編輯選項的DataGridView。 我做了我的代碼,但是出現了這個錯誤:
無法將參數值從DataGridViewTextBoxCell轉換為Int32
這是我的代碼:
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)
{
MessageBox.Show("Row Updated");
}
}
}
private void DGCategory_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (DGCategory.Columns[e.ColumnIndex].Name == "Edit")
{
UpdateRecored(Convert.ToInt32(e.RowIndex));
}
}
的DataGridViewCell表示DataGridView控件不是在它(即Number.Value)的值的單元格。 如果您的單元格中有一個文本框,則需要從該文本框中獲取價值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.