In
This tutorial we will learn how to delete ,update and insert data using
GridView in asp.net c#
- Bind
data to GridView column
- Edit
data in GridView
- Delete
rows from GridView
- Update
row from database
HTML Code for generating GridView UI
<div id="panel" style="height: 500px; background-color: White; padding: 10px; overflow: auto">
<h1>
<a href="../adminIndex.aspx">Back </a>
</h1>
<asp:updatepanel id="UpdatePanelService" runat="server" updatemode="Conditional">
<ContentTemplate>
<asp:GridView ID="gridService" runat="server" CssClass="EU_DataTable"
AutoGenerateColumns="false" ShowFooter="true"OnRowEditing="gridService_RowEditing"
onrowcreated="gridService_RowCreated"onrowupdating="gridService_RowUpdating">
<Columns>
<asp:TemplateField ItemStyle-Width="30px" HeaderText="SR.NO">
<ItemTemplate>
<asp:Label ID="lblID" runat="server"
Text='<%#Eval("service_id")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="600px" HeaderText="Service">
<ItemTemplate>
<asp:Label ID="lblService" runat="server" Text='<%#
Eval("service_name")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtService" runat="server" Text='<%#
Eval("service_name")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtService" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="100px" HeaderText="Service Photo">
<ItemTemplate>
<img src='<%# Eval("service_image")%>' alt='<%#
Eval("service_image")%>' height="50px"
width="50px" />
</ItemTemplate>
<EditItemTemplate>
<asp:FileUpload ID="fuService" runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:FileUpload ID="fuService" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkRemove" runat="server" CommandArgument='<%#
Eval("service_id")%>'
OnClientClick="return confirm('Do you want to delete?')"
Text="Delete"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="AddService"
/>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
<asp:UpdateProgress ID="UpdateProgress1" runat="server"
AssociatedUpdatePanelID="UpdatePanelService">
<ProgressTemplate>
Please wait image is uploaded....
</ProgressTemplate>
</asp:UpdateProgress>
</ContentTemplate>
<Triggers>
</Triggers>
</asp:updatepanel>
</div>
Binding GridView
private void _BindService()
{
try
{
List<BOService>
service = dALService.Service.ToList();
if (service.Count > 0 && service != null)
{
gridService.DataSource
= service;
gridService.DataBind();
}
}
catch (Exception)
{
throw;
}
}
Editing GridView
protected void gridService_RowEditing(object sender, GridViewEditEventArgs e)
{
try
{
gridService.EditIndex
= e.NewEditIndex;
_BindService();
}
catch (Exception)
{
throw;
}
}
Updating GridView
Updating GridView
protected void gridService_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
string servicename =
((TextBox)gridService.Rows[e.RowIndex].FindControl("txtService")).Text;
string filePath
=((FileUpload)gridService.Rows[e.RowIndex].FindControl("fuService")).FileName;
bOService.Service_name
= servicename;
bOService.Service_image
= "../images/service/" + filePath;
if (File.Exists(Server.MapPath("~/images/service/" + filePath)))
{
}
else
{
((FileUpload)gridService.FooterRow.FindControl("fuService")).SaveAs(Server.MapPath("~/images/service/" + filePath));
}
dALService.UpdateService(bOService);
_BindService();
}
catch (Exception)
{
throw;
}
}
Adding value in GridView
Adding value in GridView
protected void AddService(object sender, EventArgs e)
{
try
{
string servicename = ((TextBox)gridService.FooterRow.FindControl("txtService")).Text;
string filePath = ((FileUpload)gridService.FooterRow.FindControl("fuService")).FileName;
bOService.Service_name
= servicename;
bOService.Service_image
= "../images/service/" + filePath;
if (File.Exists(Server.MapPath("~/images/service/" + filePath)))
{
}
else
{
((FileUpload)gridService.FooterRow.FindControl("fuService")).SaveAs(Server.MapPath("~/images/service/" + filePath));
}
((FileUpload)gridService.FooterRow.FindControl("fuService")).SaveAs(Server.MapPath("~/images/service/" + filePath));
dALService.SetService(bOService);
_BindService();
}
catch (Exception)
{
throw;
}
}
0 Comments:
Post a Comment