SPGridView.OnRowCreated Method (Microsoft.SharePoint.WebControls)

Namespace: Microsoft.SharePoint.WebControls
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Syntax

Visual Basic (Declaration)
<SharePointPermissionAttribute(SecurityAction.Demand, ObjectModel:=True)> _
Protected Overrides Sub OnRowCreated ( _
    args As GridViewRowEventArgs _
)
Visual Basic (Usage)
Dim args As GridViewRowEventArgs

Me.OnRowCreated(args)
C#
[SharePointPermissionAttribute(SecurityAction.Demand, ObjectModel=true)] 
protected override void OnRowCreated (
    GridViewRowEventArgs args
)

Parameters

args

See Also

Tags :


Community Content

Content Master Ltd
SPGridView.OnRowCreated

Description

The SPGridView.OnRowCreated method is exposed as the SPGridView.RowCreated event of an SPGridView object.

If you are creating a class that inherits from SPGridView, you can override the SPGridView.OnRowCreated method, so that your code will be run everytime the RowCreated event is raised.

If you are creating a Web Part that includes an SPGridView control, you can add an event handler to the SPGridView.RowCreated event so that your code will be run when a row is created in the SPGridView control.

Usage Scenario

You will typically add a custom RowCreated event handler to an SPGridView control in a custom Web Part. You can then add code to your event handler routine to perform actions on the SPGridView rows when they are created.

The following code samples show how to:

  • Create an SPGridView control in the CreateChildControls method of a custom Web Part
  • Add a new event handler to the RowCreated event of the SPGridView
  • Bind the SPGridView control to a task list.
  • Count the number of times the custom event handler is called. This will keep track of how many rows are created. (Note: The event will be invoked for the header and footer rows of the SPGridView as well as the data rows, so the code starts the count at -2 to ensure that the number of rows are accurately represented)
  • Render the SPGridview control and write a message indicating how many rows there are in the list.

C# Code Sample

private SPGridView taskGrid;
private int rowCount = -2; //Discount the header and footer rows
protected override void CreateChildControls()
{
this.taskGrid = new SPGridView();
this.taskGrid.AutoGenerateColumns = false;
BoundField taskTitle = new BoundField();
taskTitle.DataField = "Title";
taskTitle.HeaderText = "To Do...";
this.taskGrid.Columns.Add(taskTitle);
this.taskGrid.RowCreated += new GridViewRowEventHandler(taskGrid_RowCreated);
SPWeb thisWeb = SPControl.GetContextWeb(Context);
SPList taskList = thisWeb.Lists["Tasks"];
SPDataSource listSource = new SPDataSource();
listSource.List = taskList;
this.taskGrid.DataSource = listSource;
this.taskGrid.DataBind();
this.Controls.Add(this.taskGrid);
}
void taskGrid_RowCreated(object sender, GridViewRowEventArgs e)
{
rowCount++;
}
public override void RenderControl(HtmlTextWriter writer)
{
taskGrid.RenderControl(writer);
writer.Write("There are " + rowCount.ToString() + " Tasks");
}

Visual Basic.NET Code Sample

Private taskGrid As SPGridView
Private rowCount As Int32 = -2 'Discount the header and footer rows
Protected Overrides Sub CreateChildControls()
Me.taskGrid = New SPGridView()
Me.taskGrid.AutoGenerateColumns = False
Dim taskTitle As BoundField = New BoundField()
taskTitle.DataField = "Title"
taskTitle.HeaderText = "To Do..."
Me.taskGrid.Columns.Add(taskTitle)
AddHandler Me.taskGrid.RowCreated, AddressOf taskGrid_RowCreated
Dim thisWeb As SPWeb = SPControl.GetContextWeb(Context)
Dim taskList As SPList = thisWeb.Lists("Tasks")
Dim listSource As SPDataSource = New SPDataSource()
listSource.List = taskList
Me.taskGrid.DataSource = listSource
Me.taskGrid.DataBind()
Me.Controls.Add(Me.taskGrid)
End Sub
  
Sub taskGrid_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
rowCount = rowCount + 1
End Sub
Public Overrides Sub RenderControl(ByVal writer As System.Web.UI.HtmlTextWriter)
taskGrid.RenderControl(writer)
writer.Write("There are " + rowCount.ToString() + " Tasks")
End Sub
Tags : sharepoint

Page view tracker