Sep 23, 2008

GridView RowUpdating

I was facing a problem with GridView1_RowUpdating. When I was editing a GridView cell, the new value entered in the cell was not retrieved. But at last i got the solution. Here is the solution:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="StudId" onrowcancelingedit="GridView1_RowCancelingEdit"
onrowupdating="GridView1_RowUpdating" >
<Columns>
<asp:BoundField DataField="StudId" HeaderText="ID" ReadOnly="true" ItemStyle-HorizontalAlign="Left" ItemStyle-Height="20" > /asp:BoundField>
<asp:BoundField DataField="StudName" HeaderText="StudName" ReadOnly="false" ItemStyle-HorizontalAlign="Left" ItemStyle-Height="20" >
/asp:BoundField>
<asp:CommandField ShowEditButton="true" />
/Columns>

/asp:GridView>


protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DataControlFieldCell cell = GridView1.Rows[e.RowIndex].Cells[1] as DataControlFieldCell;
GridView1.Columns[1].ExtractValuesFromCell(e.NewValues, cell, DataControlRowState.Edit, true);
foreach (string key in e.NewValues.Keys)
{
Response.Write("
"
+ key + ": " + e.NewValues[key]);

}

}

0 comments: