在嘗試使用C#.net編碼將gridviewrow.cells值的值轉換爲int32時遇到問題。請考慮以下編碼。如何將gridviewrow.cells [i]值的值轉換爲int32?
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("studentid",typeof(Int32)));
dt.Columns.Add(new DataColumn("Name",typeof(string)));
foreach (GridViewRow row in GridView1.Rows)
{
DataRow dr;
dr = dt.NewRow();
CheckBox cb = (CheckBox)row.FindControl("Chkgridselect");
if (cb.Checked)
{
//Error occurs in the following line when i try to typecast
dr["studentid"] =Convert.ToInt32(row.Cells[1]);
dr["Name"] = row.Cells[2];
dt.Rows.Add(dr);
}
}
請包括您的代碼並解釋它是如何失敗的。 – David 2010-12-09 14:02:36
也許你沒有在一個單元格中獲得Int32! – 2010-12-09 14:05:26
不應該轉換第一個單元格,索引爲0的單元格嗎?據我所知,你實際上將第二列轉換爲int。 – 2010-12-09 14:10:47