修改ASP.NET DataGrid的樣式
ASP.NET DataGrid 允許您修改成分單元格的樣式和布局,這可通過(guò)掛鉤 ItemCreated 事件來(lái)完成。該控件每次處理子項(xiàng)(頁(yè)眉、頁(yè)腳、行、頁(yè)導(dǎo)航)時(shí),該事件都會(huì)被激發(fā)。事件處理程序接收類(lèi)型為 DataGridItemEventArgs 的參數(shù),您可以從該參數(shù)提取所處理項(xiàng)目的類(lèi)型。
匯總行是 DataGrid 行,同樣,它的類(lèi)型可以是 Item 或 AlternatingItem。因此,在編寫(xiě) ItemCreated 處理程序時(shí),要確保只有在該項(xiàng)的類(lèi)型正確時(shí)才處理相應(yīng)的單元格。下面的列表概述所需的代碼。
- public void ItemCreated(Object sender, DataGridItemEventArgs e)
- {
- // Get the type of the newly created item
- ListItemType itemType = e.Item.ItemType;
- if (itemType == ListItemType.Item ||
- itemType == ListItemType.AlternatingItem)
- {
- // Get the data bound to the current row
- DataRowView drv = (DataRowView) e.Item.DataItem;
- if (drv != null)
- {
- // Check here the app-specific way to detect whether the
- // current row is a summary row
- :
- }
- }
- }
如果所創(chuàng)建的項(xiàng)是 DataGrid 項(xiàng)(或交替項(xiàng)),則可以通過(guò) DataItem 屬性訪(fǎng)問(wèn)綁定到行的數(shù)據(jù)。根據(jù) DataGrid 綁定到的對(duì)象的類(lèi)型,DataItem 屬性會(huì)指向不同的行對(duì)象。如果網(wǎng)格綁定到 DataView,會(huì)獲取 DataRowView 對(duì)象;如果該源用 DataTable 對(duì)象來(lái)表示,會(huì)獲取 DataRow 對(duì)象。在該示例應(yīng)用程序中,我使用 DataView 對(duì)象填充了網(wǎng)格。后來(lái),單行的數(shù)據(jù)對(duì)象成為 DataRowView 對(duì)象。
在擁有了數(shù)據(jù)行對(duì)象之后,可以應(yīng)用一些應(yīng)用程序特定的規(guī)則來(lái)確定該行是否為匯總行。在該示例應(yīng)用程序中,匯總行的 MyOrderID 字段設(shè)置為 –1。
- if ((int) drv["MyOrderID"] == -1)
- {
- // Modify style and layout here.
- // --> Set the background color to white and use bold font
- e.Item.BackColor = Color.White;
- e.Item.Font.Bold = true;
- }
DataGrid 現(xiàn)在看上去如下圖所示。
DataGrid 行實(shí)際上只是表中的一行。同樣,使用它可以很好地進(jìn)行單元格刪除以及其他調(diào)整。讓我們看一看如何使用跨越所有現(xiàn)有列的單一單元格來(lái)呈現(xiàn)匯總行。
- if ((int) drv["MyOrderID"] == -1)
在這三個(gè)原始單元格中,前兩個(gè)被刪除,第三個(gè)(現(xiàn)在包含索引 0)被正確對(duì)齊并跨越外部表的寬度。如果您希望在匯總行上顯示一些自定義文本,則需要做好面對(duì)其他問(wèn)題的準(zhǔn)備。
假設(shè)您需要添加一些文本以對(duì)小計(jì)進(jìn)行注釋?zhuān)遗c此同時(shí),讓小計(jì)與單個(gè)定單量出現(xiàn)在同一列中。在這種情況下,只需刪除一個(gè)單元格。
- e.Item.Cells.RemoveAt(1); // remove the order # cell
- e.Item.Cells[0].ColumnSpan = 2; // span the custID cell
- e.Item.Cells[1].HorizontalAlign = HorizontalAlign.Right;
- e.Item.Cells[0].Text = "Total is";
此代碼的結(jié)果如下所示。正如您所看到的那樣,它與您的預(yù)期結(jié)果不完全相同。匯總行的第一個(gè)單元格中并沒(méi)有您剛剛設(shè)置的文本。這是怎么回事呢?
此處需要考慮的重要一點(diǎn)是,Item 和 AlternatingItem 行均為綁定行。它們的明確文本只是在 OnItemDataBound 事件的過(guò)程中設(shè)置。您可能已經(jīng)猜到了,OnItemDataBound 事件會(huì)在創(chuàng)建該項(xiàng)之后激發(fā)。因此,在處理 ItemCreated 時(shí)分配給單元格的任何文本在后來(lái)都由某個(gè)事件以靜默方式改寫(xiě)。可通過(guò)設(shè)置 DataGrid 的 OnItemDataBound 屬性來(lái)掛鉤 OnItemDataBound 事件。
- < asp:DataGrid id="grid" runat="server"
- AutoGenerateColumns="false"
- :
- OnItemCreated="ItemCreated"
- OnItemDataBound="ItemDataBound"
- OnPageIndexChanged="PageIndexChanged">
- The structure of the code for
- ItemDataBound is shown below.
- public void ItemDataBound(Object sender, DataGridItemEventArgs e)
- {
- DataRowView drv = (DataRowView) e.Item.DataItem;
- if (drv == null)
- return;
- if ((int) drv["MyOrderID"] == -1)
- {
- if (drv["MyCustomerID"].ToString() == "(Total)")
- {
- e.Item.BackColor = Color.Yellow;
- e.Item.Cells[0].Text = "Orders total";
- }
- else
- e.Item.Cells[0].Text = "Customer subtotal";
- }
- }
最上面的一行是在黃色背景上繪制的,它顯示其他匯總行中的另一個(gè)文本。最終的 DataGrid 顯示如下。
以應(yīng)用程序特定的劑量很好地混合 SQL 代碼和 ASP.NET 技術(shù)可以實(shí)現(xiàn)有效的 Web 數(shù)據(jù)庫(kù)應(yīng)用程序。DataGrid 控件是一個(gè)前沿工具,可用來(lái)為它所提供的編程功能構(gòu)建完美而又功能強(qiáng)大的 Web 應(yīng)用程序,而且對(duì)于它所支持的自定義級(jí)別來(lái)說(shuō)用途更多。
【編輯推薦】