日期:2014-05-17  浏览次数:20900 次

拜托大家帮看看
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[1].Text);
  }
  catch (Exception)
  {

  throw;\\烦死了老是输入字符串不正确,数据库内表id是int类型呀
  }
  newRow["st_name"] = item.Cells[2].Text.ToString();
  newRow["ST_Price"] = item.Cells[3].Text.ToString();
  newRow["ST_Sales"] = item.Cells[4].Text.ToString();
  newRow["ST_Type"] = item.Cells[5].Text.ToString();
  newdtb.Rows.Add(newRow);
  }




  }
  GridView2.DataSource = newdtb;
  GridView2.DataBind();
  }

------解决方案--------------------
newRow["st_id"] = Convert.ToInt32(item.Cells[0].Text);
应该是从0开始算起。
------解决方案--------------------
newRow["st_id"] = Convert.ToInt32(item.Cells[1].Text);

-->你调试一下,看看Convert.ToInt32(item.Cells[1].Text) 得到的值是id还是name嘛~~

如果Convert.ToInt32(item.Cells[1].Text) 是id,那证明Convert.ToInt32换成int失败,
你换一种转换成int类型的方式吧!int.parse好像可以~~有时Convert.ToInt32转换无效