代码改变世界

ASP.NET DEMO Ⅲ:显示固定行数的GridView(DataGrid)

2007-05-30 21:33  晓风残月  阅读(...)  评论(... 编辑 收藏

目的:

1. 当绑定数据源不足预期行数的时候,手动补充残缺空白行

说明:

参考很多网络资源,方案有多种:

1.直接添加空白数据源

    优点:

   不需要手动维护空白行的视图状态。
    样式(如间隔行、编辑状态)完全交由GridView内部处理。
    通过参数设置,可以将此部分代码交由后台程序完成甚至由Stored Procedure完成,而UI部分则很统一。
    不存在GridView,DataGrid,DataList的区别。

    缺点:

    a. 约束问题。
           如果你的数据源是表模型(通常用DataTable),有可能做了主键、非空、唯一性等约束,包含空白数据的DataRow可能无法加入  DataTable;

           如果你的数据源是实体(领域)模型(通常用ArrayList或者List<T>存储),那么的实体构造函数或者属性可能做了某些对空数据的验证;

    b. 资源浪费

          凭空添加了几条空数据进来,特别当是使用实例模型的时候,你的实体实例化可能很耗资源。
          当然,这种性能损失很多时候可以不用考虑的^_^

2.手动添加空白GridViewRow

     优点:

    克服 1 中的缺点

    缺点:

    不具备1的优点。
    特别是需要手动维护空白行的视图状态,否则回发页面的时候,如果不重新绑定,将丢失空白行。

3.开发自己的GridView使其具备此特性

   缺点:成本太高。

   1: <%@ Page Language="C#" %>
   2: <%@ Import Namespace="System.Data" %>
   3:  
   4: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   5:  
   6: <script runat="server">
   1:   
   2:  
   3:     // 固定显示的行数  
   4:     const int FixedRowCount = 16;
   5:     
   6:     void Page_Load(object sender, EventArgs e)
   7:     {
   8:         if (!Page.IsPostBack) {
   9:             LoadProductData();
  10:             LoadProductData2();            
  11:         }
  12:     }
  13:  
  14:     void btnPostBack_Click(object sender, EventArgs e)
  15:     {
  16:         FillEmptyRow();
  17:     }
  18:  
  19:     void LoadProductData()
  20:     {
  21:         DataTable dt = CreateProductTable();        
  22:         
  23:         // 补充空白行        
  24:         for (int i = 0; i < dt.Rows.Count % FixedRowCount; i++) {
  25:             dt.Rows.Add(dt.NewRow());
  26:         }
  27:         
  28:         grdPro.DataSource = dt;
  29:         grdPro.DataBind();        
  30:     }
  31:  
  32:     void LoadProductData2()
  33:     {
  34:         DataTable dt = CreateProductTable();
  35:         
  36:         grdPro2.DataSource = dt;
  37:         grdPro2.DataBind();
  38:  
  39:         ViewState["__ActualRowCount_" + grdPro2.ID] = grdPro2.Rows.Count;
  40:         //
  41:         FillEmptyRow();
  42:     }
  43:  
  44:     void FillEmptyRow()
  45:     {
  46:         if (ViewState["__ActualRowCount_" + grdPro2.ID] == null) return;
  47:         int actualRowCount = (int)ViewState["__ActualRowCount_" + grdPro2.ID];
  48:         int dummyRowCount = FixedRowCount - actualRowCount;
  49:         if (dummyRowCount <= 0) return;
  50:         int colCount = grdPro2.Rows[0].Cells.Count;
  51:         DataControlRowState rowState =
  52:                 (grdPro2.Rows[actualRowCount - 1].RowState);
  53:             
  54:         for (int i = 0; i < dummyRowCount; i++) {
  55:             // 正确显示交叉行样式
  56:             rowState = (i%2 != 0)? DataControlRowState.Alternate : DataControlRowState.Normal;            
  57:             GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.DataRow, rowState);
  58:             for (int j = 0; j < colCount; j++) {
  59:                 TableCell cell = new TableCell();
  60:                 cell.Text = "&nbsp;";
  61:                 row.Cells.Add(cell);
  62:             }
  63:             // 无法直接往 grdPro2.Rows 添加
  64:             // 存在行表头,故不能使用 
  65:             // grdPro2.Controls[0].Controls.AddAt(row);            
  66:             grdPro2.Controls[0].Controls.AddAt(actualRowCount + i + 1, row);            
  67:         }
  68:     }   
  69:  
  70:     #region sample data   
  71:  
  72:     static DataTable CreateProductTable()
  73:     {
  74:         DataTable tbl = new DataTable("Products");
  75:  
  76:         tbl.Columns.Add("ProductID", typeof(int));
  77:         tbl.Columns.Add("ProductName", typeof(string));
  78:         tbl.Columns.Add("CategoryID", typeof(int));
  79:         DataRow row = tbl.NewRow();
  80:         row[0] = 1;
  81:         row[1] = "Chai";
  82:         row[2] = 1;
  83:         tbl.Rows.Add(row);
  84:  
  85:         row = tbl.NewRow();
  86:         row[0] = 2;
  87:         row[1] = "Chang";
  88:         row[2] = 1;
  89:         tbl.Rows.Add(row);
  90:  
  91:         row = tbl.NewRow();
  92:         row[0] = 3;
  93:         row[1] = "Aniseed Syrup";
  94:         row[2] = 2;
  95:         tbl.Rows.Add(row);
  96:  
  97:         row = tbl.NewRow();
  98:         row[0] = 4;
  99:         row[1] = "Chef Anton's Cajun Seasoning";
 100:         row[2] = 2;
 101:         tbl.Rows.Add(row);
 102:  
 103:         row = tbl.NewRow();
 104:         row[0] = 5;
 105:         row[1] = "Chef Anton's Gumbo Mix";
 106:         row[2] = 2;
 107:         tbl.Rows.Add(row);
 108:  
 109:         row = tbl.NewRow();
 110:         row[0] = 47;
 111:         row[1] = "Zaanse koeken";
 112:         row[2] = 3;
 113:         tbl.Rows.Add(row);
 114:  
 115:         row = tbl.NewRow();
 116:         row[0] = 48;
 117:         row[1] = "Chocolade";
 118:         row[2] = 3;
 119:         tbl.Rows.Add(row);
 120:  
 121:         row = tbl.NewRow();
 122:         row[0] = 49;
 123:         row[1] = "Maxilaku";
 124:         row[2] = 3;
 125:         tbl.Rows.Add(row);
 126:  
 127:         return tbl;
 128:     }
 129:    
 130:     #endregion
</script>
   7:  
   8: <html xmlns="http://www.w3.org/1999/xhtml">
   9: <head id="Head1" runat="server">
  10:   <title>显示固定行数的 GridView</title>
  11: </head>
  12: <body>
  13:   <form id="form1" runat="server">  
  14:     <h1 style="text-align:center">显示固定行数的 GridView</h1>
  15:     <div style="float:left">
  16:     <h2>直接填充数据源</h2>
  17:     <asp:GridView ID="grdPro" runat="server" ShowFooter="True"
  18:       Font-Size="12px" CellPadding="4" ForeColor="#333333" GridLines="None">
  19:       <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
  20:       <Columns>
  21:       </Columns>
  22:         <FooterStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True" />
  23:         <RowStyle ForeColor="#333333" BackColor="#F7F6F3" />
  24:         <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
  25:         <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
  26:         <EditRowStyle BackColor="#999999" />
  27:         <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
  28:     </asp:GridView>
  29:     </div>
  30:     <div style="float:left;padding-left:20px;">
  31:     <h2>手动填充GridView</h2>
  32:     <asp:GridView ID="grdPro2" runat="server" ShowFooter="True"
  33:       Font-Size="12px" CellPadding="4" ForeColor="#333333" GridLines="None">
  34:       <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
  35:       <Columns>
  36:       </Columns>
  37:         <FooterStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True" />
  38:         <RowStyle ForeColor="#333333" BackColor="#F7F6F3" />
  39:         <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
  40:         <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
  41:         <EditRowStyle BackColor="#999999" />
  42:         <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
  43:     </asp:GridView>
  44:     </div>  
  45:     <div style="float:none">
  46:     <asp:Button ID="btnPostBack" runat="server" Text="PostBack" OnClick="btnPostBack_Click" />
  47:     </div>  
  48:   </form>
  49: </body>
  50: </html>
完整源码下载:
GridView版本
DataGrid版本