修改ASP.NET DataGrid的樣式
ASP.NET DataGrid 允許您修改成分單元格的樣式和布局,這可通過掛鉤 ItemCreated 事件來完成。該控件每次處理子項(頁眉、頁腳、行、頁導(dǎo)航)時,該事件都會被激發(fā)。事件處理程序接收類型為 DataGridItemEventArgs 的參數(shù),您可以從該參數(shù)提取所處理項目的類型。
匯總行是 DataGrid 行,同樣,它的類型可以是 Item 或 AlternatingItem。因此,在編寫 ItemCreated 處理程序時,要確保只有在該項的類型正確時才處理相應(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)建的項是 DataGrid 項(或交替項),則可以通過 DataItem 屬性訪問綁定到行的數(shù)據(jù)。根據(jù) DataGrid 綁定到的對象的類型,DataItem 屬性會指向不同的行對象。如果網(wǎng)格綁定到 DataView,會獲取 DataRowView 對象;如果該源用 DataTable 對象來表示,會獲取 DataRow 對象。在該示例應(yīng)用程序中,我使用 DataView 對象填充了網(wǎng)格。后來,單行的數(shù)據(jù)對象成為 DataRowView 對象。
在擁有了數(shù)據(jù)行對象之后,可以應(yīng)用一些應(yīng)用程序特定的規(guī)則來確定該行是否為匯總行。在該示例應(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 行實際上只是表中的一行。同樣,使用它可以很好地進(jìn)行單元格刪除以及其他調(diào)整。讓我們看一看如何使用跨越所有現(xiàn)有列的單一單元格來呈現(xiàn)匯總行。
- if ((int) drv["MyOrderID"] == -1)
在這三個原始單元格中,前兩個被刪除,第三個(現(xiàn)在包含索引 0)被正確對齊并跨越外部表的寬度。如果您希望在匯總行上顯示一些自定義文本,則需要做好面對其他問題的準(zhǔn)備。
假設(shè)您需要添加一些文本以對小計進(jìn)行注釋,而且與此同時,讓小計與單個定單量出現(xiàn)在同一列中。在這種情況下,只需刪除一個單元格。
- 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é)果不完全相同。匯總行的第一個單元格中并沒有您剛剛設(shè)置的文本。這是怎么回事呢?
此處需要考慮的重要一點是,Item 和 AlternatingItem 行均為綁定行。它們的明確文本只是在 OnItemDataBound 事件的過程中設(shè)置。您可能已經(jīng)猜到了,OnItemDataBound 事件會在創(chuàng)建該項之后激發(fā)。因此,在處理 ItemCreated 時分配給單元格的任何文本在后來都由某個事件以靜默方式改寫??赏ㄟ^設(shè)置 DataGrid 的 OnItemDataBound 屬性來掛鉤 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";
- }
- }
最上面的一行是在黃色背景上繪制的,它顯示其他匯總行中的另一個文本。最終的 DataGrid 顯示如下。
以應(yīng)用程序特定的劑量很好地混合 SQL 代碼和 ASP.NET 技術(shù)可以實現(xiàn)有效的 Web 數(shù)據(jù)庫應(yīng)用程序。DataGrid 控件是一個前沿工具,可用來為它所提供的編程功能構(gòu)建完美而又功能強大的 Web 應(yīng)用程序,而且對于它所支持的自定義級別來說用途更多。
【編輯推薦】