C# 在datagrid视图中编辑行时出错
我有一个带有编辑选项的DataGridView。我编写了代码,但出现了以下错误: 无法将参数值从DataGridViewTextBoxCell转换为Int32 这是我的密码:C# 在datagrid视图中编辑行时出错,c#,.net,winforms,datagridview,C#,.net,Winforms,Datagridview,我有一个带有编辑选项的DataGridView。我编写了代码,但出现了以下错误: 无法将参数值从DataGridViewTextBoxCell转换为Int32 这是我的密码: private void UpdateRecored(int rowindex) { int rowaffected = 0; DataGridViewCell Number = (DataGridViewCell)DGCategory.Rows[rowindex].Cells[1]; DataGridViewCel
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)。如果单元格中有文本框,则需要从文本框中获取值 哪条语句出错?updateRecorded(Convert.ToInt32(e.RowIndex));为什么需要进行这种转换?e、 RowIndex已经是一个int。可能他正在尝试更新rowindexIs空白的记录,这在您居住的地方很昂贵?为什么代码缩进得如此稀疏?在哪一点上会出现错误,您可以进行调试和判断