protected void Button1_Click(object sender, EventArgs e)
    {        DataTable newdtb = new DataTable();
        newdtb.Columns.Add("st_id", typeof(int));
        newdtb.Columns.Add("st_name", typeof(string));
        newdtb.Columns.Add("ST_Price", typeof(string));
        newdtb.Columns.Add("ST_Sales", typeof(string));
        newdtb.Columns.Add("ST_Type", typeof(string));        newdtb.Columns["st_id"].AutoIncrement = true;
        
        foreach (GridViewRow item in GridView1.Rows)
        {
            CheckBox chkID = (CheckBox)item.FindControl("CheckBox1");
            if (chkID.Checked == true &&  item.RowType == DataControlRowType.DataRow)
            {
                                    DataRow newRow = newdtb.NewRow();
                    try
                    {                        newRow["st_id"] = Convert.ToInt32(item.Cells[0].Text);
                    }
                    catch (Exception)
                    {                        throw;//输入字符串错误
                    }
                    newRow["st_name"] = item.Cells[1].Text.ToString();
                    newRow["ST_Price"] = item.Cells[2].Text.ToString();
                    newRow["ST_Sales"] = item.Cells[3].Text.ToString();
                    newRow["ST_Type"] = item.Cells[4].Text.ToString();
                    newdtb.Rows.Add(newRow);
                }
               
              
                        }
            GridView2.DataSource = newdtb;
            GridView2.DataBind();
        }
检查数据库的ID是int类型呀!

解决方案 »

  1.   

    newdtb.Columns["st_id"].AutoIncrement = true;
    newRow["st_id"] = Convert.ToInt32(item.Cells[0].Text);
      

  2.   

    设置断点,F11单步
    检查item.Cells[0].Text值
      

  3.   

    调试看看item.Cells[0].Text的值。是不是Int的字符串,是不是有空格???
      

  4.   

    newRow["st_id"] = Convert.ToInt32(item.Cells[0].Text);
    加断点调试,监视下数据,看是否是自己想要的.这才是解决这个问题的根本.
      

  5.   

    int.Parse(item.Cells[0].FormatValue.ToString())
      

  6.   

    还是不成,转换不多来,数据库的表id是int类型的,为什么还不可以呢!以前也是这么写代码的,这次怎么就出问题了呢
      

  7.   

    newRow["st_id"] = Convert.ToInt32(item.Cells[0].Text.ToString().Trim())