facing problem while trying to typecast the value of of gridviewrow.cells value into int32 using C#.net.Please consider the following coding.
DataTable dt = 开发者_JAVA百科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);
}
}
Just a stab in the dark without more info. But try:
int myValue;
if (!int.TryParse(myGridViewRow.Cells[i].Text, out myValue)
//do something, your value is not an int
Adding some code would help, but I'm going to take a stab and say to try this:
int myVal = Int32.Parse(GridViewRow.Cells[i].Text);
int result;
result = int.Parse(value);
// OR
result = Convert.Int32(value);
DataGridViewCell has a Value property.
....
dr["studentid"] = Convert.ToInt32(row.Cells[1].Value);
....
精彩评论