注意:此类在 .NET Framework 2.0 版中是新增的。
在表中显示数据源的值,其中每列表示一个字段,每行表示一条记录。GridView 控件允许您选择和编辑这些项以及对它们进行排序。
命名空间:System.Web.UI.WebControls
程序集:System.Web(在 system.web.dll 中)

<ControlValuePropertyAttribute("SelectedValue")> _ Public Class GridView Inherits CompositeDataBoundControl Implements IPostBackContainer, IPostBackEventHandler, ICallbackContainer, ICallbackEventHandler
Dim instance As GridView
[ControlValuePropertyAttribute("SelectedValue")] public class GridView : CompositeDataBoundControl, IPostBackContainer, IPostBackEventHandler, ICallbackContainer, ICallbackEventHandler
[ControlValuePropertyAttribute(L"SelectedValue")] public ref class GridView : public CompositeDataBoundControl, IPostBackContainer, IPostBackEventHandler, ICallbackContainer, ICallbackEventHandler
/** @attribute ControlValuePropertyAttribute("SelectedValue") */ public class GridView extends CompositeDataBoundControl implements IPostBackContainer, IPostBackEventHandler, ICallbackContainer, ICallbackEventHandler
ControlValuePropertyAttribute("SelectedValue") public class GridView extends CompositeDataBoundControl implements IPostBackContainer, IPostBackEventHandler, ICallbackContainer, ICallbackEventHandler

GridView 控件用来在表中显示数据源的值。每列表示一个字段,而每行表示一条记录。GridView 控件支持下面的功能:
-
绑定至数据源控件,如
-
内置排序功能。
-
内置更新和删除功能。
-
内置分页功能。
-
内置行选择功能。
-
以编程方式访问 GridView 对象模型以动态设置属性、处理事件等。
-
多个键字段。
-
用于超链接列的多个数据字段。
-
可通过主题和样式进行自定义的外观。
![]() |
---|
如果您熟悉 .NET Framework 1.0 版中的 |
列字段
GridView 控件中的每一列由一个
DataControlField 对象表示。默认情况下,AutoGenerateColumns 属性被设置为 true,为数据源中的每一个字段创建一个 AutoGeneratedField 对象。每个字段然后作为 GridView 控件中的列呈现,其顺序同于每一字段在数据源中出现的顺序。
通过将 AutoGenerateColumns 属性设置为 false,然后定义您自己的列字段集合,您也可以手动控制哪些列字段将显示在 GridView 控件中。不同的列字段类型决定控件中各列的行为。下表列出了可以使用的不同列字段类型。
列字段类型 | 说明 |
---|---|
显示数据源中某个字段的值。这是 GridView 控件的默认列类型。 | |
为 GridView 控件中的每个项显示一个命令按钮。这使您可以创建一列自定义按钮控件,如“添加”按钮或“移除”按钮。 | |
为 GridView 控件中的每一项显示一个复选框。此列字段类型通常用于显示具有布尔值的字段。 | |
显示用来执行选择、编辑或删除操作的预定义命令按钮。 | |
将数据源中某个字段的值显示为超链接。此列字段类型允许您将另一个字段绑定到超链接的 URL。 | |
为 GridView 控件中的每一项显示一个图像。 | |
根据指定的模板为 GridView 控件中的每一项显示用户定义的内容。此列字段类型允许您创建自定义的列字段。 |
若要以声明方式定义列字段集合,请首先在 GridView 控件的开始和结束标记之间添加 <Columns> 开始和结束标记。接着,列出您想包含在 <Columns> 开始和结束标记之间的列字段。指定的列将以所列出的顺序添加到 Columns 集合中。Columns 集合存储该控件中的所有列字段,并允许您以编程方式管理 GridView 控件中的列字段。
显式声明的列字段可与自动生成的列字段结合在一起显示。两者同时使用时,先呈现显式声明的列字段,再呈现自动生成的列字段。
![]() |
---|
自动生成的列字段不会添加到 Columns 集合中。 |
绑定到数据
GridView 控件可绑定到数据源控件(如 SqlDataSource、ObjectDataSource 等等),以及实现 System.Collections.IEnumerable 接口的任何数据源(如 System.Data.DataView、System.Collections.ArrayList 或 System.Collections.Hashtable)。使用以下方法之一将 GridView 控件绑定到适当的数据源类型:
-
若要绑定到某个数据源控件,请将 GridView 控件的 DataSourceID 属性设置为该数据源控件的 ID 值。GridView 控件自动绑定到指定的数据源控件,并且可利用该数据源控件的功能来执行排序、更新、删除和分页功能。这是绑定到数据的首选方法。
-
若要绑定到某个实现 System.Collections.IEnumerable 接口的数据源,请以编程方式将 GridView 控件的 DataSource 属性设置为该数据源,然后调用 DataBind 方法。当使用此方法时,GridView 控件不提供内置的排序、更新、删除和分页功能。需要使用适当的事件提供此功能。
有关数据绑定的更多信息,请参见 通过 ASP.NET 访问数据。
![]() |
---|
此控件可用来显示用户输入,而该输入可能包含恶意的客户端脚本。在应用程序中显示从客户端发送来的任何信息之前,请检查它们是否包含可执行脚本、SQL 语句或其他代码。只要有可能,强烈建议在这些值显示在此控件中之前对它们进行 HTML 编码(默认情况下,BoundField 类会对值进行 HTML 编码)。ASP.NET 提供输入请求验证功能以阻止用户输入中的脚本和 HTML。还提供验证服务器控件以判断用户输入。有关更多信息,请参见 验证控件介绍。 |
数据操作
GridView 控件提供了很多内置功能,这些功能使得用户可以对控件中的项进行排序、更新、删除、选择和分页。当 GridView 控件绑定到某个数据源控件时,GridView 控件可利用该数据源控件的功能并提供自动排序、更新和删除功能。
![]() |
---|
GridView 控件可为其他类型的数据源提供对排序、更新和删除的支持;但是,您必须提供一个适当的事件处理程序,其中包含对这些操作的实现。 |
排序允许用户通过单击某个特定列的标题来根据该列排序 GridView 控件中的项。若要启用排序,请将 AllowSorting 属性设置为 true。
当单击 ButtonField 或 TemplateField 列字段中命令名分别为“Edit”、“Delete”和“Select”的按钮时,自动更新、删除和选择功能启用。如果 AutoGenerateEditButton、AutoGenerateDeleteButton 或 AutoGenerateSelectButton 属性分别设置为 true 时,GridView 控件可自动添加带有“编辑”、“删除”或“选择”按钮的 CommandField 列字段。
![]() |
---|
GridView 控件不直接支持将记录插入数据源。但是,通过将 GridView 控件与 DetailsView 或 FormView 控件结合使用则可以插入记录。有关更多信息,请分别参见 DetailsView 或 FormView。 |
GridView 控件可自动将数据源中的所有记录分成多页,而不是同时显示这些记录。若要启用分页,请将 AllowPaging 属性设置为 true。
自定义用户界面
您可以通过设置 GridView 控件的不同部分的样式属性自定义该控件的外观。下表列出了不同的样式属性。
样式属性 | 说明 |
---|---|
GridView 控件中的交替数据行的样式设置。当设置了此属性时,数据行交替使用 RowStyle 设置和 AlternatingRowStyle 设置进行显示。 | |
GridView 控件中正在编辑的行的样式设置。 | |
当数据源不包含任何记录时,GridView 控件中显示的空数据行的样式设置。 | |
GridView 控件的脚注行的样式设置。 | |
GridView 控件的标题行的样式设置。 | |
GridView 控件的页导航行的样式设置。 | |
RowStyle | GridView 控件中的数据行的样式设置。当还设置了 AlternatingRowStyle 属性时,数据行交替使用 RowStyle 设置和 AlternatingRowStyle 设置进行显示。 |
GridView 控件中的选中行的样式设置。 |
也可以显示或隐藏控件的不同部分。下表列出控制显示或隐藏哪些部分的属性。
属性 | 说明 |
---|---|
显示或隐藏 GridView 控件的页脚节。 | |
显示或隐藏 GridView 控件的页眉节。 |
事件
GridView 控件提供多个您可以对其进行编程的事件。这使您可以在每次发生事件时都运行一个自定义例程。下表列出了 GridView 控件支持的事件。
事件 | 说明 |
---|---|
在单击某一页导航按钮时,但在 GridView 控件处理分页操作之后发生。此事件通常用于以下情形:在用户定位到该控件中的另一页之后,您需要执行某项任务。 | |
在单击某一页导航按钮时,但在 GridView 控件处理分页操作之前发生。此事件通常用于取消分页操作。 | |
在单击某一行的“取消”按钮时,但在 GridView 控件退出编辑模式之前发生。此事件通常用于停止取消操作。 | |
当单击 GridView 控件中的按钮时发生。此事件通常用于在控件中单击按钮时执行某项任务。 | |
当在 GridView 控件中创建新行时发生。此事件通常用于在创建行时修改行的内容。 | |
在 GridView 控件中将数据行绑定到数据时发生。此事件通常用于在行绑定到数据时修改行的内容。 | |
在单击某一行的“删除”按钮时,但在 GridView 控件从数据源中删除相应记录之后发生。此事件通常用于检查删除操作的结果。 | |
在单击某一行的“删除”按钮时,但在 GridView 控件从数据源中删除相应记录之前发生。此事件通常用于取消删除操作。 | |
发生在单击某一行的“编辑”按钮以后,GridView 控件进入编辑模式之前。此事件通常用于取消编辑操作。 | |
发生在单击某一行的“更新”按钮,并且 GridView 控件对该行进行更新之后。此事件通常用于检查更新操作的结果。 | |
发生在单击某一行的“更新”按钮以后,GridView 控件对该行进行更新之前。此事件通常用于取消更新操作。 | |
发生在单击某一行的“选择”按钮,GridView 控件对相应的选择操作进行处理之后。此事件通常用于在该控件中选定某行之后执行某项任务。 | |
发生在单击某一行的“选择”按钮以后,GridView 控件对相应的选择操作进行处理之前。此事件通常用于取消选择操作。 | |
在单击用于列排序的超链接时,但在 GridView 控件对相应的排序操作进行处理之后发生。此事件通常用于在用户单击用于列排序的超链接之后执行某个任务。 | |
在单击用于列排序的超链接时,但在 GridView 控件对相应的排序操作进行处理之前发生。此事件通常用于取消排序操作或执行自定义的排序例程。 |
辅助功能
默认情况下为此控件呈现的标记可能不符合 Web 内容辅助功能准则 1.0 (WCAG) 中优先级为 1 的准则等辅助功能标准。有关此控件的辅助功能支持的详细信息,请参见 ASP.NET 控件和辅助功能。
Topic | Location |
---|---|
如何:以声明方式创建 ASP.NET Web 控件模板 | 生成 ASP .NET Web 应用程序 |
如何:使用 AccessDataSource 控件连接到 Access 数据库 (Visual Studio) | 在 Visual Studio 中构建 ASP .NET Web 应用程序 |
如何:使用设计器创建 Web 服务器控件模板 | 在 Visual Studio 中构建 ASP .NET Web 应用程序 |
如何:创建 ASP.NET Web 服务器控件模板 | 生成 ASP .NET Web 应用程序 |
如何:动态创建 ASP.NET Web 服务器控件模板 | 生成 ASP .NET Web 应用程序 |
如何:动态设置 GridView Web 服务器控件列宽 | 生成 ASP .NET Web 应用程序 |
如何:响应 DataList、Repeater 或 GridView 项中的按钮事件 | 生成 ASP .NET Web 应用程序 |
如何:在 GridView Web 服务器控件中启用默认分页 | 在 Visual Studio 中构建 ASP .NET Web 应用程序 |
如何:在 GridView Web 服务器控件中启用默认删除功能 | 在 Visual Studio 中构建 ASP .NET Web 应用程序 |
如何:在 GridView Web 服务器控件中启用默认编辑 | 在 Visual Studio 中构建 ASP .NET Web 应用程序 |
如何:在 GridView Web 服务器控件中启用默认选定内容 | 在 Visual Studio 中构建 ASP .NET Web 应用程序 |
如何:在模板化控件中绑定到数据 | 生成 ASP .NET Web 应用程序 |
如何:绑定到 Visual Studio 的模板化控件中的数据 | 在 Visual Studio 中构建 ASP .NET Web 应用程序 |
如何:自定义控件以便在 GridView Web 服务器控件中进行编辑 | 在 Visual Studio 中构建 ASP .NET Web 应用程序 |
演练:GridView Web 服务器控件的简单排序 | 在 Visual Studio 中构建 ASP .NET Web 应用程序 |
演练:使用 DetailsView 服务器控件在网页上编辑和插入数据 | 在 Visual Studio 中构建 ASP .NET Web 应用程序 |
演练:使用转换在 Web 窗体页中显示 XML 文档 | 在 Visual Studio 中构建 ASP .NET Web 应用程序 |
演练:具有 Visual Studio 数据组件的数据绑定网页 | 在 Visual Studio 中构建 ASP .NET Web 应用程序 |
演练:创建嵌套的 GridView 控件 | 在 Visual Studio 中构建 ASP .NET Web 应用程序 |
演练:创建网页以显示 Access 数据库数据 | 在 Visual Studio 中构建 ASP .NET Web 应用程序 |
演练:创建网页以显示 XML 数据 | 在 Visual Studio 中构建 ASP .NET Web 应用程序 |
演练:到自定义业务对象的数据绑定 | 在 Visual Studio 中构建 ASP .NET Web 应用程序 |
演练:在 GridView Web 服务器控件中使用存储过程显示数据 | 在 Visual Studio 中构建 ASP .NET Web 应用程序 |
演练:在 GridView Web 服务器控件中编辑时显示下拉列表 | 在 Visual Studio 中构建 ASP .NET Web 应用程序 |
演练:在 Visual Studio 中创建主/详细信息网页 | 在 Visual Studio 中构建 ASP .NET Web 应用程序 |
演练:在 Visual Web Developer 中创建和使用 ASP.NET 母版页 | 在 Visual Studio 中构建 ASP .NET Web 应用程序 |
演练:对绑定到 GridView Web 服务器控件的行执行批量更新 | 在 Visual Studio 中构建 ASP .NET Web 应用程序 |
演练:网页中的基本数据访问 | 在 Visual Studio 中构建 ASP .NET Web 应用程序 |

下面的代码示例演示如何使用 GridView 控件显示 Pubs 示例数据库(在 Microsoft SQL Server 中)的 Authors 表中的值。这些值是使用 SqlDataSource 控件检索的。
<%@ Page language="VB" %> <html> <body> <form runat="server"> <h3>GridView Example</h3> <asp:gridview id="CustomersGridView" datasourceid="CustomersSource" autogeneratecolumns="true" emptydatatext="No data available." allowpaging="true" 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="CustomersSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>
<%@ Page language="C#" %> <html> <body> <form runat="server"> <h3>GridView Example</h3> <asp:gridview id="CustomersGridView" datasourceid="CustomersSource" autogeneratecolumns="true" emptydatatext="No data available." allowpaging="true" 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="CustomersSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>
下面的代码示例演示如何使用 GridView 控件编辑记录。
<%@ Page language="VB" %> <html> <body> <form runat="server"> <h3>GridView Edit Example</h3> <!-- The GridView control automatically sets the columns --> <!-- specified in the datakeynames property as read-only. --> <!-- No input controls are rendered for these columns in --> <!-- edit mode. --> <asp:gridview id="CustomersGridView" datasourceid="CustomersSqlDataSource" autogeneratecolumns="true" autogeneratedeletebutton="true" autogenerateeditbutton="true" datakeynames="CustomerID" 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]" updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)" deletecommand="Delete from Customers where CustomerID = @CustomerID" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"> </asp:sqldatasource> </form> </body> </html>
<%@ Page language="C#" %> <html> <body> <form runat="server"> <h3>GridView Edit Example</h3> <!-- The GridView control automatically sets the columns --> <!-- specified in the datakeynames property as read-only. --> <!-- No input controls are rendered for these columns in --> <!-- edit mode. --> <asp:gridview id="CustomersGridView" datasourceid="CustomersSqlDataSource" autogeneratecolumns="true" autogeneratedeletebutton="true" autogenerateeditbutton="true" datakeynames="CustomerID" 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]" updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)" deletecommand="Delete from Customers where CustomerID = @CustomerID" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"> </asp:sqldatasource> </form> </body> </html>


- AspNetHostingPermission 用于在宿主环境中进行操作。要求值:
- InheritanceDemand;权限值:Minimal。



.NET Framework
受以下版本支持:2.0
参考
System.Web.UI.WebControls 命名空间
BoundField 类
ButtonField 类
CheckBoxField 类
CommandField 类
DataGrid 类
DetailsView 类
FormView 类
HyperLinkField
ImageField
SqlDataSource
TemplateField
AllowPaging
AllowSorting
AutoGenerateColumns
AutoGenerateEditButton
AutoGenerateSelectButton
Columns
System.Web.HttpServerUtility.HtmlEncode
其他资源
通过 ASP.NET 访问数据数据源控件概述
对数据源控件使用参数