In this article we will learn
1. To add rows to a gridview from footer
2. To edit a particular row in a gridview
3. To delete a particular row in a gridview
For this I am using a student database.
Create a gridview as follows:
<asp:GridView ID="GVStudentMarksList" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" AutoGenerateColumns="false" ShowFooter="true"
onrowcommand="GVStudentMarksList_RowCommand"
onrowdatabound="GVStudentMarksList_RowDataBound"
onrowediting="GVStudentMarksList_RowEditing"
onrowupdating="GVStudentMarksList_RowUpdating"
onrowdeleting="GVStudentMarksList_RowDeleting">
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#E3EAEB" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="StudentNo">
<ItemTemplate>
<asp:Label ID="lbStNo" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"StNo") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtStNo" runat="server" Width="100px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="StudentName">
<ItemTemplate>
<asp:Label ID="lbStName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"StName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtStName" runat="server" Width="100px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Course1">
<ItemTemplate>
<asp:Label ID="lbCourse1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Course1") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditCourse1" runat="server" Width="100px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtCourse1" runat="server" Width="100px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Course2">
<ItemTemplate>
<asp:Label ID="lbCourse2" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Course2") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditCourse2" runat="server" Width="100px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtCourse2" runat="server" Width="100px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Grade">
<ItemTemplate>
<asp:Label ID="lbGrade" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Grade") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Link" UpdateText="Update" CancelText="Cancel" EditText="Edit" ShowEditButton="true" />
<asp:TemplateField ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" Text="Delete" runat="server" CommandName="delete"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAdd" runat="server" Text="ADD" CommandName="add" BackColor="Maroon" BorderStyle="None" Font-Bold="True" ForeColor="White" Width="50px" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Initially there will be no rows in the gridview. In .net 2.0 when there are no rows in the grid then it is not visible. so we cannot add rows from a footer row. So for this I created an empty row and added to the grid. now the footer row is visible.
How to add an empty row to a gridview:
protected void EmptyGridview()
{
DataTable dtStudent = new DataTable();
DataColumn c1 = new DataColumn("StNo");
DataColumn c2 = new DataColumn("StName");
DataColumn c3 = new DataColumn("Course1");
DataColumn c4 = new DataColumn("Course2");
DataColumn c5 = new DataColumn("Grade");
dtStudent.Columns.Add(c1);
dtStudent.Columns.Add(c2);
dtStudent.Columns.Add(c3);
dtStudent.Columns.Add(c4);
dtStudent.Columns.Add(c5);
dtStudent.Rows.Add(dtStudent.NewRow());
this.GVStudentMarksList.DataSource = dtStudent;
GVStudentMarksList.DataBind();
int columncount = GVStudentMarksList.Rows[0].Cells.Count;
GVStudentMarksList.Rows[0].Cells.Clear();
GVStudentMarksList.Rows[0].Cells.Add(new TableCell());
GVStudentMarksList.Rows[0].Cells[0].ColumnSpan = columncount;
GVStudentMarksList.Rows[0].Cells[0].Text = "Add Student's data";
Session["Studentdt"] = dtStudent;
}
Handling Gridview Events:
Adding row to gridview (Row_command event)
protected void GVStudentMarksList_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "add")
{
string StNo = ((TextBox)(GVStudentMarksList.FooterRow.FindControl("txtStNo"))).Text;
string StName = ((TextBox)(GVStudentMarksList.FooterRow.FindControl("txtStName"))).Text;
string Course1 = ((TextBox)(GVStudentMarksList.FooterRow.FindControl("txtCourse1"))).Text;
string Course2 = ((TextBox)(GVStudentMarksList.FooterRow.FindControl("txtCourse2"))).Text;
string Grade = "";
int total = int.Parse(Course1) + int.Parse(Course2);
int avg = total / 2;
if (avg >= 90)
{
Grade = "A";
}
else if (avg >= 80)
{
Grade = "B";
}
else
{
Grade = "C";
}
DataTable dt = (DataTable)(Session["Studentdt"]);
DataRow dr = dt.NewRow();
dr["StNo"] = StNo;
dr["StName"] = StName;
dr["Course1"] = Course1;
dr["Course2"] = Course2;
dr["Grade"] = Grade;
dt.Rows.Add(dr);
if (GVStudentMarksList.Rows[0].Cells[0].Text == "Add Student's data")
{
dt.Rows[0].Delete();
}
Session["Studentdt"] = dt;
populategrid();
}
}
Handling Row Edit event: (Row_Edit Event)
protected void GVStudentMarksList_RowEditing(object sender, GridViewEditEventArgs e)
{
GridViewRow row = GVStudentMarksList.Rows[e.NewEditIndex];
Session["Course1"] = ((Label)(row.FindControl("lbCourse1"))).Text;
Session["Course2"] = ((Label)(row.FindControl("lbCourse2"))).Text;
GVStudentMarksList.EditIndex = e.NewEditIndex;
populategrid();
}
In this I am editing only marks:
Handling Row_DataBound:
protected void GVStudentMarksList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (((e.Row.RowState & DataControlRowState.Edit) > 0))
{
((TextBox)(e.Row.FindControl("txtEditCourse1"))).Text = Session["Course1"].ToString();
((TextBox)(e.Row.FindControl("txtEditCourse2"))).Text = Session["Course2"].ToString();
}
}
Handling Row Update Event:
protected void GVStudentMarksList_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GVStudentMarksList.Rows[e.RowIndex];
string Course1 = ((TextBox)(row.FindControl("txtEditCourse1"))).Text;
string Course2 = ((TextBox)(row.FindControl("txtEditCourse2"))).Text;
string StNo = ((Label)(row.FindControl("lbStNo"))).Text;
DataTable Studentdt = (DataTable)(Session["Studentdt"]);
DataRow StudentRow;
string Grade = "";
int total = int.Parse(Course1) + int.Parse(Course2);
int avg = total / 2;
if (avg >= 90)
{
Grade = "A";
}
else if (avg >= 80)
{
Grade = "B";
}
else
{
Grade = "C";
}
Studentdt.Rows[e.RowIndex]["Course1"] = Course1;
Studentdt.Rows[e.RowIndex]["Course2"] = Course2;
Studentdt.Rows[e.RowIndex]["Grade"] = Grade;
Session["Studentdt"] = Studentdt;
GVStudentMarksList.EditIndex = -1;
populategrid();
}
Handling Row Delete event
protected void GVStudentMarksList_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataTable studentdt=(DataTable)(Session["Studentdt"]);
studentdt.Rows[e.RowIndex].Delete();
Session["Studentdt"] = studentdt;
populategrid();
}
protected void populategrid()
{
if (Session["Studentdt"] != null)
{
DataTable dt = (DataTable)(Session["Studentdt"]);
GVStudentMarksList.DataSource = dt;
GVStudentMarksList.DataBind();
}
if (GVStudentMarksList.Rows.Count == 0)
{
EmptyGridview();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
populategrid();
}
}
Let Me Know If Needed Source Code.
0 Comments Received
Post a Comment