| Gridview Databound Event |
|
We use gridview databound event when we want to do certain things after the server controls bind to the datasource. For example after the binding we may want to display some text at the bottom of the grid or we might want to calculate the total value of a column. THIS IS AN ASPX FILE: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DGridview.aspx.cs" Inherits="_DGridview" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>GridView Demo</title> </head> <body> <form id="form1" runat="server"> <div> <h1>GridView Demo</h1>
<asp:GridView ID="GridDataBound" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1"> <Columns> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" /> <asp:BoundField DataField="MarksInMaths" HeaderText="MarksInMaths" SortExpression="MarksInMaths" /> <asp:BoundField DataField="MarksInEnglish" HeaderText="MarksInEnglish" SortExpression="MarksInEnglish" /> <asp:BoundField DataField="MarksInScience" HeaderText="MarksInScience" SortExpression="MarksInScience" /> <asp:BoundField DataField="TotalMarks" HeaderText="TotalMarks" SortExpression="TotalMarks" /> <asp:BoundField DataField="ObtainedMarks" HeaderText="ObtainedMarks" SortExpression="ObtainedMarks" /> </Columns> </asp:GridView>
</div> <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetStudents" TypeName="QVISLib.SampleData"> </asp:ObjectDataSource> </form> </body> </html>
THIS IS THE CODE FILE:
using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq;
public partial class _DGridview : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //Create a new databound event GridDataBound.DataBound += new EventHandler(GridDataBound_DataBound); }
void GridDataBound_DataBound(object sender, EventArgs e) { GridDataBound.ShowFooter = true;
//Remove un-use GridDataBound for (int i = GridDataBound.Columns.Count - 1; i > 0; i--) { GridDataBound.FooterRow.Cells.RemoveAt(i); }
GridDataBound.FooterRow.Cells[0].ColumnSpan = GridDataBound.Columns.Count; GridDataBound.FooterRow.Cells[0].HorizontalAlign = HorizontalAlign.Center;
GridDataBound.FooterRow.Cells[0].Text = "This is an example of data bound event"; } }
HERE IS THE OUTPUT
|
