April 25, 2011

Insert,Update,Delete in GridView in ASP.Net

<asp:GridView ID="gvSearchResults" runat="server" BackColor="White" BorderColor="#CCCCCC"
DataKeyNames="ID" BorderStyle="None" BorderWidth="1px" CellPadding="3" AutoGenerateColumns="false"
EmptyDataText="No Data Found" EmptyDataRowStyle-HorizontalAlign="Center" EmptyDataRowStyle-BackColor="AliceBlue"
Width="100%" Height="50%" OnRowCancelingEdit="gvSearchResults_RowCancelingEdit"
OnRowDeleting="gvSearchResults_RowDeleting" OnRowEditing="gvSearchResults_RowEditing"
OnRowUpdating="gvSearchResults_RowUpdating">

On CS Page

protected void gvSearchResults_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string ID = gvSearchResults.DataKeys[e.RowIndex].Values[0].ToString();
DeleteDetails(ID);
SearchResults();

}

protected void gvSearchResults_RowEditing(object sender, GridViewEditEventArgs e)
{

gvSearchResults.EditIndex = e.NewEditIndex;
SearchResults();

}

protected void gvSearchResults_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string ID = gvSearchResults.DataKeys[e.RowIndex].Values[0].ToString();

TextBox txtCTNo = (TextBox)gvSearchResults.Rows[e.RowIndex].FindControl("txtCTNo");
TextBox txtZFNo = (TextBox)gvSearchResults.Rows[e.RowIndex].FindControl("txtZFNo");

// Call Update details function here.

gvSearchResults.EditIndex = -1;
SearchResults();
}

No comments:

Post a Comment