All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
PHP一些常用的正则表达式_php资料_编程技术
CDocument类的UpdateAllViews()成员函数
ASP.NET2.0数据库入门之常见错误_[Asp.Net教程]
ASP.NET,2.0中合并,GridView,的表头单元格_[Asp.Net教程]
关于textarea的直观换行_JavaScript技术_编程技术
PHP环境下配置在线编辑器FCKeditor_php资料_编程技术
Sql,server传送数组参数的变通办法_[SQL,Server教程]
分页,SQLServer存储过程_[SQL,Server教程]
LINQ,性能分析系列之传说中的LINQ_.net资料_编程技术
ASP.NET服务器控件PleaseWaitButton_[Asp.Net教程]
Reading number is top 10 pictures
湖边的风景
西游四格漫画(一)
恶搞漫画1
A cat have life principles
Go to the national museum2
Look at the Spring Festival people crowded into the what kind
这酸爽,让人不敢相信
Parking technology is great, that give you the keys can't stolen
Average female college students2
So beauty, will let you spray blood9
Download software ranking
C#COM编程指南
Sora aoi‘s film--Lust fan wall
VeryCD电驴(EasyMule) V1.1.9 Build09081
Eclipse 4.2.1 For Win32
Boxer vs Yellow3
好色的外科大夫
C#编程思想
Unix video tutorial19
豪门浪荡史
少妇苏霞全本
delv published in(发表于) 2014/1/16 9:32:52 Edit(编辑)
实现类似Windows资源管理器的DataGrid_[Asp.Net教程]

实现类似Windows资源管理器的DataGrid_[Asp.Net教程]

实现类似Windows资源管理器的DataGrid_[Asp.Net教程]























在DataGrid中,我们可以实现类似Windows资源管理器的效果,即对列进行排序,该列颜色与其它列不同。下面就是实现的代码:




C#代码:




DataGridLikeWindowsExplorer.aspx




复制ASPX代码保存代码<%@ Page language="c#" Codebehind="DataGridLikeWindowsExplorer.aspx.cs"
AutoEventWireup="false" Inherits="aspxWebCS.DataGridLikeWindowsExplorer" %>



DataGridLikeWindowsExplorer















Northwind职员表


BorderStyle="None" BorderWidth="5px" BackColor="White" CellPadding="5" AllowSorting="True"
AutoGenerateColumns="False" AllowPaging="True" GridLines="Horizontal" PageSize="5">








DataFormatString="{0:d}"/>









<%@ Page language="c#" Codebehind="DataGridLikeWindowsExplorer.aspx.cs"
AutoEventWireup="false" Inherits="aspxWebCS.DataGridLikeWindowsExplorer" %>



DataGridLikeWindowsExplorer















Northwind职员表


BorderStyle="None" BorderWidth="5px" BackColor="White" CellPadding="5" AllowSorting="True"
AutoGenerateColumns="False" AllowPaging="True" GridLines="Horizontal" PageSize="5">








DataFormatString="{0:d}"/>










DataGridLikeWindowsExplorer.aspx.cs
复制C#代码保存代码using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.Caching;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;




namespace aspxWebCS
{
///


/// DataGridLikeWindowsExplorer 的摘要说明。
///

public class DataGridLikeWindowsExplorer : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid WinExplorerView_DataGrid;
protected System.Data.SqlClient.SqlDataAdapter m_sqlDataAdapter;
protected System.Data.SqlClient.SqlCommand m_sqlSelectCommand;
protected System.Data.SqlClient.SqlConnection m_sqlConnection;
protected System.Data.DataSet m_dsEmployees;
protected System.Data.DataView m_dvEmployees;
protected System.Collections.Hashtable colHeaderMap;




protected string m_strSortExpr;
protected string m_strSortOrder;
protected int m_iSortColumnIdx;
protected System.Web.UI.WebControls.PlaceHolder Tips_PlaceHolder;




protected string strConn = "Data Source=.;User Id=sa;Password=;Initial Catalog=Northwind;";




private void Page_Load(object sender, System.EventArgs e)
{
m_iSortColumnIdx = -1;
m_strSortExpr = "";
m_strSortOrder = "";
ProcessViewState();
PrepareColumnHeaderMap();




if (!IsPostBack)
{
BindGridToView();
}
}




private void PrepareColumnHeaderMap()
{
colHeaderMap = new Hashtable();
int idx = 0;
foreach (DataGridColumn col in WinExplorerView_DataGrid.Columns)
{
colHeaderMap[col.SortExpression] = idx++;
}
}




private void RetrieveData()
{
if (null == Cache["EmployeesDS"])
{
string tmp = "SELECT LastName, FirstName, Title, BirthDate, City FROM Employees";
m_sqlConnection = new SqlConnection(strConn);
m_sqlSelectCommand = new SqlCommand(tmp, m_sqlConnection);
m_sqlDataAdapter = new SqlDataAdapter(m_sqlSelectCommand);
m_dsEmployees = new DataSet("Employees");
m_sqlDataAdapter.Fill(m_dsEmployees);
Cache.Insert("EmployeesDS", m_dsEmployees, null, DateTime.Now.AddMinutes(1), Cache.NoSlidingExpiration);
}
else
{
m_dsEmployees = (DataSet) Cache["EmployeesDS"];
}
}




private void ProcessViewState()
{
if (null != ViewState["SortExpr"])
{
m_strSortExpr = ViewState["SortExpr"].ToString();
}
if (null != ViewState["SortOrder"])
{
m_strSortOrder = ViewState["SortOrder"].ToString();
}
}




private void BindGridToView()
{
string strSort = "";
if (0 != m_strSortExpr.Length)
{
strSort = m_strSortExpr;
if (0 != m_strSortOrder.Length)
{
strSort += (" " + m_strSortOrder);
}
}
RetrieveData();




m_dvEmployees = new DataView(m_dsEmployees.Tables[0], "", strSort, DataViewRowState.CurrentRows);
WinExplorerView_DataGrid.DataSource = m_dvEmployees;
WinExplorerView_DataGrid.DataBind();
}




private Color GetSortColumnColor()
{
if (null == this.m_strSortOrder ||
String.Empty == this.m_strSortOrder ||
0 == this.m_strSortOrder.Length)
{
return Color.Gold;
}




if (m_strSortOrder.CompareTo("ASC") == 0)
{
return Color.Gold;
}
else
{
return Color.BlanchedAlmond;
}
}




private void OnPageIndexChange(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
WinExplorerView_DataGrid.CurrentPageIndex = e.NewPageIndex;
BindGridToView();
}




private void OnSortView(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
m_strSortExpr = e.SortExpression;
ViewState["SortExpr"] = m_strSortExpr;




if (0 == m_strSortOrder.Length)
{
m_strSortOrder = "ASC";
}
else if (m_strSortOrder.CompareTo("ASC") == 0)
{
m_strSortOrder = "DESC";
}
else
{
m_strSortOrder = "ASC";
}
ViewState["SortOrder"] = m_strSortOrder;
// 找到Click事件所在的列序号
m_iSortColumnIdx = Convert.ToInt32(this.colHeaderMap[m_strSortExpr]);
BindGridToView();
}




private void OnItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
int idx = 0;
foreach (TableCell cl in e.Item.Cells)
{
cl.Attributes.Add("onmouseover", "showheadertip(" + idx.ToString() + ");");
cl.Attributes.Add("onmouseout", "hideheadertip(" + idx.ToString() + ");");
idx++;
}
}
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
if (-1 != m_iSortColumnIdx)
{
e.Item.Cells[m_iSortColumnIdx].BackColor = GetSortColumnColor();
}
}
}




protected override void OnPreRender(EventArgs e)
{
///ToolTip文字
int nCount = this.WinExplorerView_DataGrid.Columns.Count;
for (int i = 0; i < nCount; i++)
{
Panel pnl = new Panel();
pnl.CssClass = "gridtooltip";
pnl.ID = "htip" + i.ToString();
Literal lt = new Literal();
lt.Text = this.GetHeaderTooltipText(i);
pnl.Controls.Add(lt);
this.Tips_PlaceHolder.Controls.Add(pnl);
}
base.OnPreRender(e);
}




private string GetHeaderTooltipText(int iColIdx)
{
switch (iColIdx)
{
case 0:
return "职员的姓";
case 1:
return "职员名字";
case 2:
return "职员的职位";
case 3:
return "出生日期";
case 4:
return "居住地";
default:
throw new ArgumentException("无效地列序号", "Index");
}
}




#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}




private void InitializeComponent()
{
this.WinExplorerView_DataGrid.ItemCreated +=
new System.Web.UI.WebControls.DataGridItemEventHandler(this.OnItemCreated);
this.WinExplorerView_DataGrid.PageIndexChanged +=
new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.OnPageIndexChange);
this.WinExplorerView_DataGrid.SortCommand +=
new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.OnSortView);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}




来源:阿良.NET













































添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.