Below design code and
aspx.cs code is useful for Inserting,
Updating and Deleting records to Database
(Delete Data into DB) by using ASP.NET Gridview.
The GridView control is
a feature rich and versatile control used to accept, display, and edit data on
a web page. It is a commonly used control in ASP.Net web applications.
To use a GridView control a DataSource control has to be attached to the GridView control. The property DataSourceID of the GridView control binds the GridView control to the DataSource control and allows paging, sorting and database operations with the DataSource.
To use a GridView control a DataSource control has to be attached to the GridView control. The property DataSourceID of the GridView control binds the GridView control to the DataSource control and allows paging, sorting and database operations with the DataSource.
Below is the design code for all operations of the gridview(
Insert or Save, Update, Delete, EmptyDataTemplate records save).
ASPX GridVive design:
<asp:GridView ID="gvContact" runat="server" AutoGenerateColumns="false"
OnRowCommand="gvContact_RowCommand" OnRowEditing="gvContact_RowEditing"
AllowPaging="true" DataKeyNames="PersonSk" ShowFooter="false"
OnRowCancelingEdit=" gvContact_RowCancelingEdit" OnRowEditing="gvContact_RowEditing" OnRowUpdating="gvContact_RowUpdating" OnRowDataBound="gvContact_RowDataBound" OnRowDeleting="DeleteRecord" >
<Columns>
<asp:TemplateField HeaderText="Operations">
<ItemTemplate>
<span>
<asp:LinkButton Text="Edit" ID="Edit" runat="server" CommandArgument="<%# Container.DataItemIndex %>"
CommandName="Edit" ToolTip="Edit" />
<asp:LinkButton ID="lnkBDelete" runat="Server" Text="Delete" CommandName="Delete" CommandArgument="<%#
Container.DataItemIndex %>"></asp:LinkButton>
</span>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton Text="Update" ID="Update" ForeColor="Red" runat="server" CommandArgument="<%# Container.DataItemIndex %>"
CommandName="Update" ToolTip="Update" ValidationGroup="Update" /> <asp:LinkButton
Text="Cancel" ID="Cancel" ForeColor="Red" runat="server" CommandArgument="<%#
Container.DataItemIndex %>"
CommandName="Cancel" ToolTip="Cancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton runat="server" ID="btnAdd" Text="Add" CommandArgument="<%# Container.DataItemIndex %>"
CommandName="Add" ToolTip="Add" ValidationGroup="Add" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Main No"
HeaderStyle-CssClass="CenterAlign" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID='lblMainNo' runat="Server" Text='<%# Eval("MainNo") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID='txtMainNoET' runat="Server" Width="150px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID='txtMainNoFT' runat="Server" Width="150px">
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" HeaderStyle-CssClass="CenterAlign" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID='lblName' runat="Server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID='txtNameET' runat="Server" Width="150px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID='txtNameFT' runat="Server" Width="150px">
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email"
HeaderStyle-CssClass="CenterAlign" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID='lblEmail' runat="Server" Text='<%# Eval("Email") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID='txtEmailET' runat="Server" Width="150px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID='txtEmailFT' runat="Server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<th align="left" scope="col">
<asp:Label ID="lblEDTMainno" runat="server" CssClass="Headings"
Text="Main No." />
</th>
<th align="left" scope="col">
<asp:Label ID="lblEDTName" runat="server" CssClass="Headings"
Text="Name" />
</th>
<th align="left" scope="col">
<asp:Label ID="lblEDTEmail" runat="server" CssClass="Headings"
Text="Email ID" />
</th>
<th align="left" scope="col">
<asp:Label ID="lblEDTOperation"
runat="server" CssClass="Headings" Text="Operation" />
</th>
<th scope="col"></th>
</tr>
<tr>
<td align="left">
<asp:TextBox runat="server" ID="txtMainNoEDT" MaxLength="13" Width="120px"></asp:TextBox>
</td>
<td align="left">
<asp:TextBox runat="server" ID="txtNameEDT" Width="120px"></asp:TextBox>
</td>
<td align="left">
<asp:TextBox runat="server" ID="txtEmailEDT" Width="120px"></asp:TextBox>
</td>
<td>
<table border="0" cellpadding="0" width="30%" cellspacing="0">
<tr>
<td width="2%">
<asp:Button ID="btnEDTAdd" Text="ADD" runat="server" CommandArgument="<%# Container.DataItemIndex %>"
CommandName="EmptyDataSave" />
</td>
<td>
<asp:Button ID="btnEDTCancel" Text="Cancel" runat="server" CommandName="EmptyDataCancel" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</EmptyDataTemplate>
</asp:GridView>
Below total code of all operations of the grid
view(Insert or save, Update, Delete, EmptyDataTemplate records save).
protected void gvContact_RowCommand(object
sender, GridViewCommandEventArgs e)
{
if
(e.CommandName == "Add")
{
DataTable
dtADDMessage = new DataTable();
TextBox
txtMainNoFT = this.gvContact.FooterRow.FindControl("txtMainNoFT") as
TextBox;
TextBox
txtNameFT = this.gvContact.FooterRow.FindControl("txtNameFT") as
TextBox;
TextBox
txtEmailFT = this.gvContact.FooterRow.FindControl("txtEmailFT") as
TextBox;
objclass.ActionTender = 1;
objclass.MainNo = txtMainNoFT.Text;
objclass.Name = txtNameFT.Text;
objclass.Email = txtEmailFT.Text;
dtADDMessage = objclass.AddInfo();
if
((dtADDMessage == null))
{
gvContact.EditIndex = -1;
gvContact.DataSource =
dtADDMessage;
gvContact.DataBind();
string
message = "Data Saved successfully !";
string
script = "<script
language=\"javascript\"
type=\"text/javascript\">;alert('" + message + "');</script>";
ScriptManager.RegisterStartupScript(Page,
this.GetType(), "AlertMessage",
script, false);
}
else
{
}
}
if (e.CommandName ==
"EmptyDataSave")
{
DataTable dtUpdateMessage = new
DataTable();
TextBox txtMainNoEDT =
this.gvContact.Controls[0].Controls[0].FindControl("txtMainNoEDT") as
TextBox;
TextBox txtNameEDT =
this.gvContact.Controls[0].Controls[0].FindControl("txtNameEDT") as
TextBox;
TextBox txtEmailEDT =
this.gvContact.Controls[0].Controls[0].FindControl("txtEmailEDT") as
TextBox;
objclass.ActionTender = 1;
objclass.MainNo =
txtMainNoEDT.Text;
objclass.Name = txtNameEDT.Text;
objclass.Email = txtEmailEDT.Text;
dtUpdateMessage =
objclass.AddInfo();
if ((dtUpdateMessage == null))
{
gvContact.EditIndex = -1;
gvContact.DataSource =
dtUpdateMessage;
gvContact.DataBind();
string message = "Data
Saved successfully !";
string script = "<script
language=\"javascript\"
type=\"text/javascript\">;alert('" + message +
"');</script>";
ScriptManager.RegisterStartupScript(Page, this.GetType(),
"AlertMessage", script, false);
}
else
{
}
}
if (e.CommandName ==
"EmptyDataCancel")
{
DataTable dtUpdateMessage = new
DataTable();
dtUpdateMessage =
objclass.GetData();
gvContact.DataSource =
dtUpdateMessage;
gvContact.DataBind();
}
if
(e.CommandName == "Update")
{
DataTable
dtuPdATEMessage = new DataTable();
TextBox
txtMainNoET = this.gvContact.Rows[this.gvContact.EditIndex].FindControl("txtMainNoET") as
TextBox;
TextBox
txtNameET = this.gvContact.Rows[this.gvContact.EditIndex].FindControl("txtNameET") as
TextBox;
TextBox
txtEmailET = this.gvContact.Rows[this.gvContact.EditIndex].FindControl("txtEmailET") as
TextBox;
objclass.ActionTender = 2;
objclass.MainNo = txtMainNoET.Text;
objclass.Name = txtNameET.Text;
objclass.Email = txtEmailET.Text;
dtuPdATEMessage =
objclass.AddInfo();
if
((dtuPdATEMessage == null))
{
gvContact.EditIndex = -1;
gvContact.DataSource =
dtuPdATEMessage;
gvContact.DataBind();
string
message = "Data Saved successfully !";
string
script = "<script
language=\"javascript\"
type=\"text/javascript\">;alert('" + message + "');</script>";
ScriptManager.RegisterStartupScript(Page,
this.GetType(), "AlertMessage",
script, false);
}
else
{
}
}
if
(e.CommandName == "Cancel")
{
gvContact.EditIndex = -1;
dtADDMessage = objclass.AddInforGrid();
gvContact.DataSource =
dtADDMessage;
gvContact.DataBind();
}
if
(e.CommandName == "Delete")
{
DataTable dtDeleteMessage = new DataTable();
int deleteRowID = Convert.ToInt32(e.CommandArgument);
int PersonSk = Convert.ToInt32(gvContact.DataKeys[(deleteRowID)
- (this.gvContact.PageIndex *
20)].Value.ToString());
objclass.ActionTender
= 3;
objclass.PersonSk =
PersonSk;
dtDeleteMessage =
objclass.DeleteInfo();
if ((dtDeleteMessage == null))
{
gvContact.EditIndex = -1;
gvContact.DataSource = dtDeleteMessage;
gvContact.DataBind();
string message = "Data Deleted
successfully !";
string script = "<script
language=\"javascript\"
type=\"text/javascript\">;alert('" + message + "');</script>";
ScriptManager.RegisterStartupScript(Page,
this.GetType(), "AlertMessage", script, false);
}
else
{
}
}
}
protected void grdTCMMain_RowDataBound(object
sender, GridViewRowEventArgs e)
{
if
(e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton
btnDelete = (LinkButton)e.Row.FindControl("lnkBDelete");
if
(btnDelete != null)
{
btnDelete.Attributes.Add("onclick", "javascript:return
" +
"confirm('Are
you sure you want to delete this record?')");
}
}
}
No comments:
Post a Comment