使用ASP.NET 2.0 GridView创建自定义排序用户界面

0 下载量 55 浏览量 更新于2024-08-31 收藏 101KB PDF 举报
ASP.NET 2.0中创建自定义排序用户界面 在ASP.NET 2.0中,创建自定义排序用户界面是非常重要的,因为它可以帮助用户更容易地区分不同组的数据。特别是在显示大量已经按类别排序的数据时,没有类别分界线,用户很难找到所需要的类别。因此,本文将主要讲解如何在ASP.NET 2.0中使用GridView创建自定义排序,主要演示在一组相同类别的数据前添加分界行,让用户更容易区分不同组的数据。 首先,需要创建一个普通的、能够排序的GridView。在创建增强型排序用户界面之前,需要先创建一个普通的列出所有产品的GridView并且能够排序。可以在PagingAndSorting文件夹下的CustomSortingUI.aspx添加一个GridView,设置ID="ProductList",以一个ObjectDataSource为数据源,ObjectDataSource的数据从ProductsBLL类的GetProducts()取得。 接下来,需要设置GridView的列,包括ProductName,CategoryName,SupplierName,UnitPrice绑定列和Discontinued复选框列,再设置GridView允许排序。设置完这些以后,可以在代码编辑中看到以下代码: <asp:GridView ID="ProductList" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ProductID" DataSourceID="ObjectDataSource1" EnableViewState="False"> <Columns> <asp:BoundField DataField="ProductName" HeaderText="Product" /> <asp:BoundField DataField="CategoryName" HeaderText="Category" /> <asp:BoundField DataField="SupplierName" HeaderText="Supplier" /> <asp:BoundField DataField="UnitPrice" HeaderText="Unit Price" /> <asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" /> </Columns> </asp:GridView> 在设置GridView的列和排序功能后,需要添加类别分界线以便区分不同组的数据。可以在GridView的RowDataBound事件中添加类别分界线。例如,可以在RowDataBound事件中检查当前行的类别是否与前一行的类别相同,如果不同,则添加一个类别分界线。 protected void ProductList_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string currentCategory = ((Product)e.Row.DataItem).CategoryName; if (e.Row.RowIndex > 0) { GridViewRow previousRow = ProductList.Rows[e.Row.RowIndex - 1]; string previousCategory = ((Product)previousRow.DataItem).CategoryName; if (currentCategory != previousCategory) { e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml("#CCCCCC"); e.Row.Font.Bold = true; } } } } 通过添加类别分界线,可以使用户更容易地找到需要的类别。例如,在显示大量已经按类别排序的数据时,用户可以快速地找到Seafood产品的开始和结束的地方。 创建自定义排序用户界面可以帮助用户更容易地区分不同组的数据。在ASP.NET 2.0中,可以使用GridView和ObjectDataSource来创建自定义排序用户界面,并添加类别分界线以便区分不同组的数据。