| |
GeekInterview.com > Interview Questions > Microsoft > Ado.NET
| Print | |
Question: How to update a column in DataGrid using C#
Answer: I am getting InvalidCastException as (Specified cast is not valid) while updating 2nd column in a datagrid? Id,firstname,lastname are the three columns of my datagrid respectively. I wanted to edit the second column(lastname) and update it. I did the following code in DataGrid's updatecommand(),but failed to update ! Int varid=(int)DataGrid1.DataKeys[e.Item.ItemIndex]; TextBox lnm=(TextBox)e.Item.Cells[2].Controls[0]; string str=lnm.Text ; SqlCommand cmd=new SqlCommand("update customer set lastname='" + str + "' where id=" + varid + "",con); cmd.ExecuteNonQuery(); DataGrid1.EditItemIndex=-1; DataGrid1.DataBind(); |
| July 07, 2008 09:16:19 |
#2 |
| sureshnaspuri |
Member Since: July 2008 Total Comments: 2 |
RE: How to update a column in DataGrid using C# |
After seeing ur code i understood that u haven't call the bind method.
gridview1.EditItemIndex = -1; after this u call the bind method .
gridview1.databind();//in this place u call the grid bind method n try it once.
|
| |
Back To Question | |