Gautam Buddha Temple Varanasi,Varanasi - Buddhist Pilgrimage

Tuesday, August 24, 2010

Add new row using GridView footer column

Adding new record in database from Gridview footer row

Abstract:
Here we will discuss about to add new recored in database using Gridview footer column.
Just drag and drop a gridview in your page and follow the below code.This code in three tier application
so i am writing here only front end code.


<asp:GridView ID="gvStudentDetail" runat="server" DataKeyNames="StudentDetailId" AllowPaging="True" PageSize="12"
AutoGenerateColumns="False" OnPageIndexChanging="gvStudentDetail_PageIndexChanging"
OnRowDataBound="gvStudentDetail_RowDataBound" TabIndex="2" Width="697px" OnRowCancelingEdit="gvStudentDetail_RowCancelingEdit"
OnRowEditing="gvStudentDetail_RowEditing" OnRowUpdating="gvStudentDetail_RowUpdating"
ShowFooter="True" OnRowCommand="gvStudentDetail_RowCommand" OnRowDeleting="gvStudentDetail_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="Sr. No">
<ItemTemplate>
<asp:Label ID="lblSrNo" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="StudentDetailId" Visible="False">
<ItemTemplate>
<asp:Label ID="lblStudentDetailId" runat="server" Text='<%# Bind("StudentDetailId") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblFooterStudentDetailId" runat="server"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="StudentDetail">
<ItemTemplate>
<asp:Label ID="lblStudentDetail" runat="server" Text='<%# Bind("StudentDetail") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtStudentDetail" runat="server" Text='<%# Bind("StudentDetail") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewStudentDetail" runat="server" Visible="false"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:ImageButton ID="ImgYes" runat="server" ImageUrl="~/Image/image_edit.png" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
ImageUrl="~/Image/imageupdate.png" Text="Update" />
<asp:ImageButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
ImageUrl="~/Image/imagecancel.png" Text="Cancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="ButtonAdd" runat="server" ImageUrl="~/Image/imagerow_add.png" CommandName="AddNew"
ToolTip="Add New Record" />
<asp:ImageButton ID="lbtnAdd" runat="server" CausesValidation="True" CommandName="Add"
ImageUrl="~/Image/imageadd.png" Visible="false" Text="Add" />
<asp:ImageButton ID="lbtnCancel" runat="server" CausesValidation="False" CommandName="Cancel"
ImageUrl="~/Image/imagecancel.png" Visible="false" Text="Cancel" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:ImageButton ID="ImgDelete" runat="server" ImageUrl="~/Image/image_delete.png"
CommandName="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

----------------------------------------------------------------------------------------
protected void GridViewStudentDetail()
{
try
{
Ds = objStudentDetailBLL.GetData();
if (Ds.Tables[0].Rows.Count > 0)
{
gvStudentDetail.DataSource = Ds;
gvStudentDetail.DataBind();
}
else
{
ShowNoResultFound(Ds.Tables[0], gvStudentDetail);
}
}
catch (Exception ex)
{
exceptionMessage.Text = ex.Message;
}
}
protected void gvStudentDetail_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblSrNo = (Label)e.Row.FindControl("lblSrNo");
lblSrNo.Text = SrNo.ToString();
SrNo++;
}
}
protected void gvStudentDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvStudentDetail.PageIndex = e.NewPageIndex;
GridViewStudentDetail();
}
protected void gvStudentDetail_RowEditing(object sender, GridViewEditEventArgs e)
{
StudentDetail.EditIndex = e.NewEditIndex;
GridViewStudentDetail();
}
protected void gvStudentDetail_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

objStudentDetail.StudentDetailId = Convert.ToInt32(((Label)gvStudentDetail.Rows[e.RowIndex].FindControl("lblStudentDetailId")).Text);
objStudentDetail.StudentDetail = Convert.ToString(((TextBox)gvStudentDetail.Rows[e.RowIndex].FindControl("txtStudentDetail")).Text);
try
{
int intResultUpdate = objStudentDetailBLL.Update(objStudentDetailDetail);
if (intResultUpdate > 0)
{
exception.Text = "Record Updated Successfully !";
gvStudentDetail.EditIndex = -1;
GridViewStudentDetail();
}
}
catch (Exception ee)
{
exception.Text = ee.Message.ToString();
}
finally
{
objStudentDetailBLL = null;
}

}
protected void gvStudentDetail_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvStudentDetail.EditIndex = -1;
GridViewStudentDetail();
}
protected void gvStudentDetail_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
objStudentDetail.StudentDetailId = (int)gvStudentDetail.DataKeys[e.RowIndex].Value;
int result = objStudentDetailBLL.Delete(objStudentDetailDetail);
if (result > 0)
{
exception.Text = "Record Deleted Successfully !";
gvStudentDetail.EditIndex = -1;
GridViewStudentDetail();
}
}
catch(Exception ex)
{
exceptionText = ex.Message.ToString();

}
}
protected void gvStudentDetail_RowCommand(object sender, GridViewCommandEventArgs e)
{
ImageButton lbtnAdd = (ImageButton)gvStudentDetail.FooterRow.FindControl("lbtnAdd");
ImageButton lbtnCancel = (ImageButton)gvStudentDetail.FooterRow.FindControl("lbtnCancel");
ImageButton ButtonAdd = (ImageButton)gvStudentDetail.FooterRow.FindControl("ButtonAdd");
ButtonAdd.Visible = false;

TextBox txtNewStudentDetail= (TextBox)gvStudentDetail.FooterRow.FindControl("txtNewStudentDetail");
txtNewStudentDetail.Visible = true;
lbtnCancel.Visible = true;
lbtnAdd.Visible = true;

objStudentDetailDetail.StudentDetail = Convert.ToString(((TextBox)gvStudentDetailFooterRow.FindControl("txtNewStudentDetail")).Text);

if (e.CommandName.Equals("Add"))
{
try
{
if (objStudentDetailDetail.StudentDetail == "")
{
exceptionText = "Please Enter the StudentDetail!!";
}
else
{
int intResultUpdate = objStudentDetailBLL.Insert(objStudentDetail);
if (intResultUpdate > 0)
{
exception.Text = "Record Added Successfully !";
StudentDetail.EditIndex = -1;
GridViewStudentDetail();
}
}
}
catch (Exception ee)
{
exceptionMessage.Text = ee.Message.ToString();
}
finally
{
objStudentDetailBLL = null;
}
}
}

No comments:

Post a Comment