开发者

how to typecast the value of gridviewrow.cells[i] value into int32?

开发者 https://www.devze.com 2023-01-29 10:53 出处:网络
facing problem while trying to typecast the value of of gridviewrow.cells value into int32 using C#.net.Please consider the following coding.

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);
....
0

精彩评论

暂无评论...
验证码 换一张
取 消