GridViewRowEventHandler Delegar
Definição
Importante
Algumas informações se referem a produtos de pré-lançamento que podem ser substancialmente modificados antes do lançamento. A Microsoft não oferece garantias, expressas ou implícitas, das informações aqui fornecidas.
Representa o método que manipula os eventos RowCreated e RowDataBound de um controle GridView.
public delegate void GridViewRowEventHandler(System::Object ^ sender, GridViewRowEventArgs ^ e);
public delegate void GridViewRowEventHandler(object sender, GridViewRowEventArgs e);
type GridViewRowEventHandler = delegate of obj * GridViewRowEventArgs -> unit
Public Delegate Sub GridViewRowEventHandler(sender As Object, e As GridViewRowEventArgs)
Parâmetros
- sender
- Object
A fonte do evento.
Um objeto de GridViewRowEventArgs que contém os dados do evento.
Exemplos
O exemplo a seguir demonstra como adicionar programaticamente um GridViewRowEventHandler delegado ao RowDataBound evento de um GridView controle .
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
// Create a new GridView control.
GridView customersGridView = new GridView();
// Set the GridView control's properties.
customersGridView.ID = "CustomersGridView";
customersGridView.DataSourceID = "CustomersSqlDataSource";
customersGridView.AutoGenerateColumns = true;
customersGridView.AllowPaging = true;
// Programmatically register the event-handling method.
customersGridView.RowDataBound += new GridViewRowEventHandler(this.CustomersGridView_RowDataBound);
// Add the GridView control to the Controls collection
// of the PlaceHolder control.
GridViewPlaceHolder.Controls.Add(customersGridView);
}
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Display the company name in italics.
e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridViewRowEventHandler Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridViewRowEventHandler Example</h3>
<asp:placeholder id="GridViewPlaceHolder"
runat="server"/>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
' Create a new GridView control.
Dim customersGridView As New GridView()
' Set the GridView control's properties.
customersGridView.ID = "CustomersGridView"
customersGridView.DataSourceID = "CustomersSqlDataSource"
customersGridView.AutoGenerateColumns = True
customersGridView.AllowPaging = True
' Programmatically register the event-handling method.
AddHandler customersGridView.RowDataBound, AddressOf CustomersGridView_RowDataBound
' Add the GridView control to the Controls collection
' of the PlaceHolder control.
GridViewPlaceHolder.Controls.Add(customersGridView)
End Sub
Sub CustomersGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
' Display the company name in italics.
e.Row.Cells(1).Text = "<i>" & e.Row.Cells(1).Text & "</i>"
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridViewRowEventHandler Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridViewRowEventHandler Example</h3>
<asp:placeholder id="GridViewPlaceHolder"
runat="server"/>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
O exemplo a seguir demonstra como adicionar declarativamente um GridViewRowEventHandler delegado ao RowDataBound evento de um GridView controle .
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Display the company name in italics.
e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView RowDataBound Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView RowDataBound Example</h3>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="true"
allowpaging="true"
onrowdatabound="CustomersGridView_RowDataBound"
runat="server">
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub CustomersGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
' Display the company name in italics.
e.Row.Cells(1).Text = "<i>" & e.Row.Cells(1).Text & "</i>"
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView RowDataBound Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView RowDataBound Example</h3>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="true"
allowpaging="true"
onrowdatabound="CustomersGridView_RowDataBound"
runat="server">
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
Comentários
Antes que o GridView controle possa ser renderizado, um GridViewRow objeto deve ser criado para cada linha no controle . O RowCreated evento é gerado sempre que uma linha no GridView controle é criada. Isso permite que você forneça um método de manipulação de eventos que executa uma rotina personalizada, como adicionar conteúdo personalizado a uma linha, sempre que esse evento ocorrer.
Da mesma forma, cada linha no controle deve ser associada a um registro na fonte de dados antes que o GridView controle possa ser renderizado. O RowDataBound evento é gerado quando uma linha de dados (representada por um GridViewRow objeto) é associada aos dados no GridView controle . Isso permite que você forneça um método de manipulação de eventos que executa uma rotina personalizada, como modificar os valores dos dados associados à linha, sempre que esse evento ocorrer.
Ao criar um GridViewRowEventHandler delegado, você identifica o método que manipulará o evento. Para associar o evento ao manipulador de eventos, adicione uma instância do delegado ao evento. O manipulador de eventos é chamado sempre que o evento ocorre, a menos que você remova o representante. Para obter mais informações sobre delegados do manipulador de eventos, consulte Manipulando e gerando eventos.
Métodos de Extensão
GetMethodInfo(Delegate) |
Obtém um objeto que representa o método representado pelo delegado especificado. |