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类型呀!
{ 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类型呀!
newRow["st_id"] = Convert.ToInt32(item.Cells[0].Text);
检查item.Cells[0].Text值
加断点调试,监视下数据,看是否是自己想要的.这才是解决这个问题的根本.