Advertisement
Guest User

Bruno Alexandre / DataGrid with thead and tfoot Example

a guest
Jun 15th, 2010
775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.14 KB | None | 0 0
  1. // #########################################
  2. // Default.aspx.cs File
  3. // #########################################
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Web;
  9. using System.Web.UI;
  10. using System.Web.UI.WebControls;
  11. using System.Reflection;
  12.  
  13. public partial class _Default : System.Web.UI.Page
  14. {
  15.     protected void Page_Load(object sender, EventArgs e)
  16.     {
  17.         if (!Page.IsPostBack)
  18.         {
  19.             myDataGrid dg = new myDataGrid();
  20.             dg.DataSource = new string[] { "1", "2", "3", "4", "5", "6" };
  21.             dg.DataBind();
  22.  
  23.             ph.Controls.Add(dg);
  24.         }
  25.     }
  26. }
  27.  
  28. public class myDataGrid : System.Web.UI.WebControls.DataGrid
  29. {
  30.     protected override void OnPreRender(EventArgs e)
  31.     {
  32.         Table table = Controls[0] as Table;
  33.  
  34.         if (table != null && table.Rows.Count > 0)
  35.         {
  36.             // first row is the Table Header <thead>
  37.             table.Rows[0].TableSection = TableRowSection.TableHeader;
  38.             // last row is the Footer Header <tfoot> (comment for not using this)
  39.             table.Rows[table.Rows.Count - 1].TableSection = TableRowSection.TableFooter;
  40.  
  41.             FieldInfo field = typeof(WebControl).GetField("tagKey", BindingFlags.Instance | BindingFlags.NonPublic);
  42.             foreach (TableCell cell in table.Rows[0].Cells)            
  43.                 field.SetValue(cell, HtmlTextWriterTag.Th);
  44.         }
  45.         base.OnPreRender(e);
  46.     }
  47. }
  48.  
  49.  
  50. <!--
  51. // #########################################
  52. // Default.aspx File
  53. // #########################################
  54. -->
  55.  
  56. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  57.  
  58. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  59. <html xmlns="http://www.w3.org/1999/xhtml">
  60. <head runat="server">
  61.     <title>DataGrid with thead and tfoot Example</title>
  62. </head>
  63. <body>
  64.     <form id="form1" runat="server">
  65.     <div>
  66.         <h1>
  67.             DataGrid example</h1>
  68.         <asp:PlaceHolder ID="ph" runat="server" />
  69.     </div>
  70.     </form>
  71. </body>
  72. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement