Have you ever thought how grid view handles all the events like RowDataBound, RowCommand, RowDeleting, RowUpdating, etc...? If you look in System.Web.UI.WebControls.GridView class, you will find lots of properties, events and methods.
Let’s take RowDataBound as an example to understand the GridView workflow.
1. We have an event RowDataBound which gets fired OnRowDataBound method :
// Summary: Occurs when a data row is bound to data in a
System.Web.UI.WebControls.GridView control.
public event GridViewRowEventHandler RowDataBound;
2. We have a delegate to handle RowDataBound event :
using System;
namespace System.Web.UI.WebControls
{
// Summary: Represents the method that handles the
// System.Web.UI.WebControls.GridView.RowCreated and S
// System.Web.UI.WebControls.GridView.RowDataBound events of a
// System.Web.UI.WebControls.GridView control.
// Parameters:
// sender: The source of the event.
//e: A System.Web.UI.WebControls.GridViewRowEventArgs object that contains the event data.
public delegate void GridViewRowEventHandler(object sender, GridViewRowEventArgs e);
}
3. We have a method OnRowDataBound() which raises the RowDataBound event :
// Summary: Raises the System.Web.UI.WebControls.GridView.RowDataBound event.
// Parameters:
// e: A System.Web.UI.WebControls.GridViewRowEventArgs that contains event data.
protected virtual void OnRowDataBound(GridViewRowEventArgs e);
4. Create grdTest_RowDataBound function in aspx.cs and write the code which you want to execute for each row when data binding takes and call it in OnRowDataBound()
Here I am generating the Serial No., which will be displayed in grid.
private int intSerialNo = 1;
/// Handles the RowDataBound event of the grdEmail control.
/// The source of the event.
/// The instance containing the event data.
protected void grdEmail_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ((e.Row.RowType == DataControlRowType.DataRow) || (e.Row.RowType == DataControlRowType.Footer))
{
Label serialNumber = (Label)e.Row.FindControl("lblSno");
serialNumber.Text = intSerialNo.ToString() + ".";
intSerialNo = intSerialNo + 1;
}
}
This is how GridView works!!
Saturday, October 2, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment