如何修复错误为程序增加价值?

时间:2017-10-23 19:57:37

标签: c# stored-procedures

我收到错误:

  

无法隐式将string类型转换为int

这是我的代码:

private void dataGridView1_CellValidating(object 
sender, DataGridViewCellValidatingEventArgs e)
{
    String Id = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();

     try
     {
         using (SqlConnection conn = new SqlConnection(_csData))
         {
             conn.Open();
             SqlCommand comm = new SqlCommand("updateTable", conn);
             comm.CommandType = CommandType.StoredProcedure;

             comm.Parameters.Add("@vrID", SqlDbType.VarChar).Value = Id;

             SqlDataAdapter da = new SqlDataAdapter(comm);
             da.SelectCommand = comm;
             DataTable db = new DataTable();
             da.Fill(db);

             dataGridView1.DataSource = db;

         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(String.Format("An error occurred: '{0}'", ex.Message));
     }
 }

我也试过这个:

int Id = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString());

1 个答案:

答案 0 :(得分:0)

试试这个:

private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)

 {


 int Id = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString());

  try
        {
            using (SqlConnection conn = new SqlConnection(_csData))
            {
                conn.Open();
                SqlCommand comm = new SqlCommand("updateTable", conn);
                comm.CommandType = CommandType.StoredProcedure;



                comm.Parameters.Add("@vrID", SqlDbType.Int).Value = Id;

                SqlDataAdapter da = new SqlDataAdapter(comm);
                da.SelectCommand = comm;
                DataTable db = new DataTable();
                da.Fill(db);

                dataGridView1.DataSource = db;

            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(String.Format("An error occurred: '{0}'", ex.Message));
        }
   }