GridView Row Edit and Update would be one among the basics a .Net fresher programmer to start with.
CODE BEHIND File
CODE BEHIND File
protected void GrvCannibalize_RowEditing(object sender, GridViewEditEventArgs e)
{
GrvCannibalize.EditIndex = e.NewEditIndex;
BindCannibaliseGrid();
}
protected void GrvCannibalize_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GrvCannibalize.EditIndex = -1;
BindCannibaliseGrid();
}
protected void GrvCannibalize_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TrnFutureSales oEntity = new TrnFutureSales(); // Business Entity
FutureSalesLogic oLogic = new FutureSalesLogic(); // Business Logic Layer
TextBox oWRActual = GrvCannibalize.Rows[e.RowIndex].FindControl("TxtWRActual") as TextBox;
TextBox oWIActual = GrvCannibalize.Rows[e.RowIndex].FindControl("TxtWIActual") as TextBox;
TextBox oCNNActual = GrvCannibalize.Rows[e.RowIndex].FindControl("TxtCNNActual") as TextBox;
HiddenField oFutureSalesId = GrvCannibalize.Rows[e.RowIndex].FindControl("HdnFutureSalesId") as HiddenField;
oEntity.ActualCan = Convert.ToDecimal(oWRActual.Text);
oEntity.ActualRR = Convert.ToDecimal(oCNNActual.Text);
oEntity.ActualImpact = Convert.ToDecimal(oWIActual.Text);
oEntity.FutureSalesId = Convert.ToInt64(oFutureSalesId.Value);
oLogic.UpdateFutureSalesForId(oEntity); // This method has to be added as yours to update the database.
GrvCannibalize.EditIndex = -1;
BindCannibaliseGrid(); // Bind the grid again to load with the new values.
}
ASPX (MARK UP) File
<asp:GridView ID="GrvCannibalize" DataKeyNames="FutureSalesID" runat="server" AutoGenerateColumns="false" EmptyDataText="No Record Found" EmptyDataRowStyle-CssClass="redstar" AllowPaging="true" Width="100%" PageSize="5" ShowFooter="true" OnRowEditing="GrvCannibalize_RowEditing" OnRowCancelingEdit="GrvCannibalize_RowCancelingEdit"
OnRowUpdating="GrvCannibalize_RowUpdating" AutoGenerateEditButton="false">
<Columns>
<asp:CommandField ButtonType="Image" ShowEditButton="True" EditImageUrl="~/App_Themes/images/Edit.gif" UpdateImageUrl="~/App_Themes/images/Update.gif" CancelImageUrl="~/App_Themes/images/Cancel.jpeg">
</<asp:CommandField>
<asp:TemplateField HeaderText="Actual WI">
<ItemTemplate>
<asp:Label ID="LblWIActual" runat="server" Text='<%# Bind("ActualImpact") %>'></<asp:Label>
<asp:HiddenField ID="HdnFutureSalesId" runat="server" Value='<%# Eval("FutureSalesID") %>'></<asp:HiddenField>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TxtWIActual" runat="server" CssClass="txtboxAmount" Text=''></<asp:TextBox>
<asp:RequiredFieldValidator ID="RfvWIActual" runat="server" ErrorMessage="Required" CssClass="redstar" Text="Required" ControlToValidate="TxtWIActual">
</<asp:RequiredFieldValidator>
</EditItemTemplate>
</<asp:TemplateField>
<asp:TemplateField HeaderText="Actual WR">
<ItemTemplate>
<asp:Label ID="LblWRActual" runat="server" Text='<%# Bind("ActualRR") %>'></<asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TxtWRActual" runat="server" CssClass="txtboxAmount" Text=''></<asp:TextBox>
<asp:RequiredFieldValidator ID="RfvWRActual" runat="server" ErrorMessage="Required" CssClass="redstar" Text="Required" ControlToValidate="TxtWRActual"></<asp:RequiredFieldValidator>
</EditItemTemplate>
</<asp:TemplateField>
</Columns>
</<asp:GridView>
ASPX (MARK UP) File
<asp:GridView ID="GrvCannibalize" DataKeyNames="FutureSalesID" runat="server" AutoGenerateColumns="false" EmptyDataText="No Record Found" EmptyDataRowStyle-CssClass="redstar" AllowPaging="true" Width="100%" PageSize="5" ShowFooter="true" OnRowEditing="GrvCannibalize_RowEditing" OnRowCancelingEdit="GrvCannibalize_RowCancelingEdit"
OnRowUpdating="GrvCannibalize_RowUpdating" AutoGenerateEditButton="false">
<Columns>
<asp:CommandField ButtonType="Image" ShowEditButton="True" EditImageUrl="~/App_Themes/images/Edit.gif" UpdateImageUrl="~/App_Themes/images/Update.gif" CancelImageUrl="~/App_Themes/images/Cancel.jpeg">
</<asp:CommandField>
<asp:TemplateField HeaderText="Actual WI">
<ItemTemplate>
<asp:Label ID="LblWIActual" runat="server" Text='<%# Bind("ActualImpact") %>'></<asp:Label>
<asp:HiddenField ID="HdnFutureSalesId" runat="server" Value='<%# Eval("FutureSalesID") %>'></<asp:HiddenField>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TxtWIActual" runat="server" CssClass="txtboxAmount" Text=''></<asp:TextBox>
<asp:RequiredFieldValidator ID="RfvWIActual" runat="server" ErrorMessage="Required" CssClass="redstar" Text="Required" ControlToValidate="TxtWIActual">
</<asp:RequiredFieldValidator>
</EditItemTemplate>
</<asp:TemplateField>
<asp:TemplateField HeaderText="Actual WR">
<ItemTemplate>
<asp:Label ID="LblWRActual" runat="server" Text='<%# Bind("ActualRR") %>'></<asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TxtWRActual" runat="server" CssClass="txtboxAmount" Text=''></<asp:TextBox>
<asp:RequiredFieldValidator ID="RfvWRActual" runat="server" ErrorMessage="Required" CssClass="redstar" Text="Required" ControlToValidate="TxtWRActual"></<asp:RequiredFieldValidator>
</EditItemTemplate>
</<asp:TemplateField>
</Columns>
</<asp:GridView>
No comments:
Post a Comment