XP 风格的可拖动列、可排序、可改变宽度的DataGrid的例子

类别:.NET开发 点击:0 评论:0 推荐:
XP 风格的可拖动列、可排序、可改变宽度的DataGrid的例子
作者:孟宪会 出自:【孟宪会之精彩世界】 发布日期:2004年2月16日 8点58分34秒

结合客户端脚本,我们可以创建出可以拖动列,可以拖动改变列宽度,显示、隐藏列、编辑列的XP风格的 DataGrid,下面就是所有的代码。自己调试时请注意修改eMeng.Exam.UltraGrid路径。

查看例子

UltraGrid.aspx

<%@ Page language="c#" Codebehind="UltraGrid.aspx.cs" AutoEventWireup="false" Inherits="eMeng.Exam.UltraGrid.UltraGrid" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>XP 风格的可拖动列、可排序、可改变宽度的DataGrid的例子</title> <META http-equiv="content-type" content="text/html; charset=gb2312"> <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0"> <meta name="CODE_LANGUAGE" Content="C#"> <link rel="stylesheet" type="text/css" href="UltraGrid.css"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="DragableXpStyleTable" method="post" runat="server"> <div align="center" style="PADDING:5px"> <b>&nbsp;XP 风格的可拖动列、可排序、可改变宽度的DataGrid的例子。</b> </div> <div id="coolUltraGrid1" runat="server"> <div class="gridHead"> <div class="gridRow" id="gridRow" runat="server"></div> </div> <asp:Literal id="RowItem" runat="server"></asp:Literal> </div> </form> </body> </HTML>

UltraGrid.aspx.cs

using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace eMeng.Exam.UltraGrid { /// <summary> /// UltraGrid 的摘要说明。本例子演示了如何创建可排序、可拖放、可调整宽度的XP风格的DataGrid。 /// 出自:【孟宪会之精彩世界】 /// </summary> public class UltraGrid : System.Web.UI.Page { protected System.Web.UI.HtmlControls.HtmlGenericControl gridRow; protected System.Web.UI.WebControls.Literal RowItem; protected System.Web.UI.HtmlControls.HtmlGenericControl coolUltraGrid1; private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 coolUltraGrid1.Attributes.Add("class","coolUltraGrid"); coolUltraGrid1.Attributes.Add("style","WIDTH: 100%; HEIGHT: 400"); coolUltraGrid1.Attributes.Add("borderStyle","2"); coolUltraGrid1.Attributes.Add("altRowColor","oldLace"); coolUltraGrid1.Attributes.Add("selectionStyle","1"); /// 请根据你的数据库的设置,修改这里的数据库连接字符串和查询语句。其它不要修改。 OleDbConnection cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\xxx.mdb"); cn.Open(); string sqlstring = "SELECT TOP 30 D.Title As 文档标题,D.CreateDate as 发布时间,S.Title AS 所属栏目 "; sqlstring += "FROM Document D,Subject S WHERE D.pid=S.id ORDER BY D.CreateDate DESC"; OleDbCommand cmd = new OleDbCommand(sqlstring,cn); OleDbDataReader dr; dr=cmd.ExecuteReader(CommandBehavior.CloseConnection); /// ///组合表头 /// int FieldNumber = dr.FieldCount; int ColWidth,Tmp = 0; if(FieldNumber == 0) Response.End(); ColWidth = (int)100/FieldNumber; string TableHeader = ""; for(int i=0;i<FieldNumber;i++) { if(dr.Read()) { if(i==FieldNumber-1) TableHeader +="<span width='"+(100-Tmp).ToString()+"%'>"+dr.GetName(i).ToString()+" <p>本文地址:http://com.8s8s.com/it/it43210.htm</p> </div> </div> </div> <div class="content_tag"> <script src="/js/module_astro_input.js" type="text/javascript"></script> </div> </div> <div class="tech_show"> <script type="text/javascript">module_love_728x90();</script> </div> <div class="tech_footer"> <p>Copyright &copy; 2004-2013 信息技术中文网 All rights reserved 版权所有</p> <p>Powered by 信息技术中文网 Ver2.0</p> </div> </div> <script type="text/javascript">module_footer();</script> </body> </html>