Gridview 多重表头 (二)
多重表头之排序
这是个有点忧桑的故事。。。Cynthia告诉我,研究一个问题,我们不可能有超过一天的时间。。。
结果好好几天过去鸟~~还没有完成。。。
由于不再使用Gridview自带的表头行,于是无法绑定gridview自带排序方法。只能根据点击列名做不同处理。
我的思路是酱滴,将点击的列名存在一个hiddenfield里,排序时候根据hiddenfield里存储的值作为升/降序的依据。
好丢人的说,第一次的时候存在了label里,每次回传都被label默认值覆盖,后来才发现,不是所有控件都叫Hidden field...
ASPX
<script language="javascript" type="text/javascript">
function test(o)
{
document.getElementById("Label1").innerText = o;
document.getElementById("LinkButton1").click();
} </script>
.... <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowSorting="true"
onrowcreated="GridView1_RowCreated">
<Columns>
<asp:BoundField DataField="StoreId" HeaderText="Store Id" SortExpression="StoreId" />
<asp:BoundField DataField="Requeseter" HeaderText="Requester" SortExpression="Requeseter" />
<asp:BoundField DataField="EmployeeId" HeaderText="EmployeeId" SortExpression="EmployeeId"/>
<asp:BoundField DataField="StoreId" HeaderText="StoreId2" SortExpression="StoreId" />
</Columns>
</asp:GridView> <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click"
ClientIDMode="Static">LinkButton</asp:LinkButton>
<asp:HiddenField ID="Label1" runat="server" Value=""/>
CS
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<Mix> list = new List<Mix>();
int i = ;
list.AddRange(new List<Mix> { new Mix { StoreId = , Requeseter = "A", EmployeeId = i + }, new Mix { StoreId = , Requeseter = "A", EmployeeId = i + }, new Mix { StoreId = , Requeseter = "A", EmployeeId = i + }, new Mix { StoreId = , Requeseter = "B", EmployeeId = i + } });
ViewState["lst"] = list;
GridView1.DataSource = list;
GridView1.DataBind();
ViewState["i"] = ;
}
this.LinkButton1.Style.Add("display", "none");
} protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
switch (e.Row.RowType)
{
case DataControlRowType.Header: TableCellCollection tcHeader = e.Row.Cells;
tcHeader.Clear();
//Associate
tcHeader.Add(new TableHeaderCell());
tcHeader[].BackColor = System.Drawing.Color.FromArgb(, , );
tcHeader[].ForeColor = System.Drawing.Color.White;
tcHeader[].BorderColor = System.Drawing.Color.LightGray;
tcHeader[].HorizontalAlign = HorizontalAlign.Center;
tcHeader[].Attributes.Add("rowspan", "");
tcHeader[].Text = "<br><a id='Associate' href=\"#\" onclick='test(this.id)'>Associate</a>";
// ClientScriptManager.RegisterForEventValidation();
//SecurityLevel
tcHeader.Add(new TableHeaderCell());
tcHeader[].BackColor = System.Drawing.Color.FromArgb(, , );
tcHeader[].ForeColor = System.Drawing.Color.White;
tcHeader[].BorderColor = System.Drawing.Color.LightGray;
tcHeader[].HorizontalAlign = HorizontalAlign.Center;
tcHeader[].Attributes.Add("colspan", "");
tcHeader[].Text = "<p>SecurityLevel</p></th></tr><tr>"; //2Line: Default Level
tcHeader.Add(new TableHeaderCell());
tcHeader[].BackColor = System.Drawing.Color.FromArgb(, , );
tcHeader[].ForeColor = System.Drawing.Color.White;
tcHeader[].BorderColor = System.Drawing.Color.LightGray;
tcHeader[].HorizontalAlign = HorizontalAlign.Center;
tcHeader[].Text = "<a id='jobcodedefault' href=\"#\" onclick='test(this.id)'>Job Code Default</a>";
//tcHeader[2].Text = "<p>Job Code Default</p>"; //2Line: Current Level
tcHeader.Add(new TableHeaderCell());
tcHeader[].BackColor = System.Drawing.Color.FromArgb(, , );
tcHeader[].ForeColor = System.Drawing.Color.White;
tcHeader[].BorderColor = System.Drawing.Color.LightGray;
tcHeader[].HorizontalAlign = HorizontalAlign.Center;
//<a id='jobcodedefault' href=\"#\" onclick='test(this.id)'>Job Code Default</a>
tcHeader[].Text = "<a id='current' href=\"#\" onclick='test(this.id)'>Current</a>"; //tcHeader[3].Text = "<label style='width:99%; text-align:center; vertical-align:middle; color:White; font-weight:bold '>Current</label>";
tcHeader.Add(new TableHeaderCell()); //2Line: Requested Level
tcHeader[].BackColor = System.Drawing.Color.FromArgb(, , );
tcHeader[].ForeColor = System.Drawing.Color.White;
tcHeader[].BorderColor = System.Drawing.Color.LightGray;
tcHeader[].HorizontalAlign = HorizontalAlign.Center;
tcHeader[].Style.Add("BorderColor", "White");
// tcHeader[4].Text = "<label style='width:99%; text-align:center; vertical-align:middle; color:White; font-weight:bold '>Requested</label></tr>";
tcHeader[].Text = "<a id='requester' href=\"#\" onclick='test(this.id)'>Requester</a></tr>"; break;
} } protected void LinkButton1_Click(object sender, EventArgs e)
{
List<Mix> list = new List<Mix>();
List<Mix> listSorted = new List<Mix>(); list = (List<Mix>)ViewState["lst"]; switch (Label1.Value)
{
case "Associate":
{
int j = (int)ViewState["i"]; if ((j++) % == )
{
var historydesc = from c in list orderby c.StoreId descending select c;
foreach (var x in historydesc)
{
listSorted.Add(x);
}
}
else
{
var historyasc = from c in list orderby c.StoreId ascending select c;
foreach (var x in historyasc)
{
listSorted.Add(x);
}
}
ViewState["i"] = j; break;
}
case "jobcodedefault":
{
int j = (int)ViewState["i"]; if ((j++) % == )
{
var historydesc= from c in list orderby c.Requeseter descending select c;
foreach (var x in historydesc)
{
listSorted.Add(x);
}
}
else
{
var historyasc = from c in list orderby c.Requeseter ascending select c;
foreach (var x in historyasc)
{
listSorted.Add(x);
}
}
ViewState["i"] = j; break;
}
case "current":
{
int j = (int)ViewState["i"]; if ((j++) % == )
{
var historydesc = from c in list orderby c.EmployeeId descending select c;
foreach (var x in historydesc)
{
listSorted.Add(x);
}
}
else
{
var historyasc = from c in list orderby c.EmployeeId ascending select c;
foreach (var x in historyasc)
{
listSorted.Add(x);
}
}
ViewState["i"] = j; break; }
case "requester":
{
int j = (int)ViewState["i"]; if ((j++) % == )
{
var historydesc = from c in list orderby c.StoreId descending select c;
foreach (var x in historydesc)
{
listSorted.Add(x);
}
}
else
{
var historyasc = from c in list orderby c.StoreId ascending select c;
foreach (var x in historyasc)
{
listSorted.Add(x);
}
}
ViewState["i"] = j; break; }
} this.GridView1.DataSource = listSorted;
this.GridView1.DataBind();
}

就算是简单的实现了吧,直到Cynthia告诉我可以酱。
lkbAssociate.Click += new EventHandler(grdHistorySort);
以Associate列为栗子。
//flag for asc and desc
private bool AssociateAsc
{
get { return (ViewState["AssociateAsc"] == null) ? true : (bool)ViewState["AssociateAsc"]; }
set { ViewState["AssociateAsc"] = value; }
}
tcHeader.Add(new TableHeaderCell());
tcHeader[].BackColor = System.Drawing.Color.FromArgb(, , );
tcHeader[].ForeColor = System.Drawing.Color.White;
tcHeader[].BorderColor = System.Drawing.Color.LightGray;
tcHeader[].HorizontalAlign = HorizontalAlign.Center;
tcHeader[].Attributes.Add("rowspan", "");
LinkButton lkbAssociate = new LinkButton();
lkbAssociate.Style.Add("text-align", "center");
lkbAssociate.Style.Add("color", "white");
lkbAssociate.ID = "lkbAssociate";
lkbAssociate.Text = "Associate (EID)";
lkbAssociate.Click += new EventHandler(grdHistorySort);
tcHeader[].Controls.Add(lkbAssociate);
protected void grdHistorySort(object sender, EventArgs e)
{
List<PMSecRequestHistoryInfo> originalList = new List<PMSecRequestHistoryInfo>();
List<PMSecRequestHistoryInfo> sortedList = new List<PMSecRequestHistoryInfo>(); originalList = (List<PMSecRequestHistoryInfo>)ViewState["InitHistoryTabList"];
LinkButton linkButonClicked = sender as LinkButton; switch (linkButonClicked.ID)
{ case "lkbAssociate":
{
if (ViewState["AssociateAsc"] == null || (bool)ViewState["AssociateAsc"])
{
var sorted = from c in originalList orderby c.DisplayName ascending select c;
foreach (var info in sorted)
{
sortedList.Add(info);
} ViewState["AssociateAsc"] = false;
}
else
{
var sorted = from c in originalList orderby c.DisplayName descending select c;
foreach (var info in sorted)
{
sortedList.Add(info);
} ViewState["AssociateAsc"] = true;
} }
break;
...
酱婶儿的,会不会简明扼要的多。。。Cynthiao(≧v≦)o~~好棒
唯一遗憾的是我不晓得如何为分隔的列添加控件同时添加格式。

这样两列,不能如法炮制。
第一次时候用了上次的写法,写了一个<a onclik></a>,发现页面要刷瞎我的双眼。。。
不闪的,才是健康的~
于是,拽了一个LinkButton,点了事件里的onclick想看看是个什么样子。ViewSource 发现是辣样一个东西,于是模仿了一个。
tcHeader[].Text = "<a id='LinkButton1' href='javascript:__doPostBack('LinkButton1','')' style='text-align:center;color:white' >Request</a></th></tr><tr>";
排序类似
protected void LinkButton1_Click(object sender, EventArgs e)
{
List<PMSecRequestHistoryInfo> originalList = new List<PMSecRequestHistoryInfo>();
List<PMSecRequestHistoryInfo> sortedList = new List<PMSecRequestHistoryInfo>(); originalList = (List<PMSecRequestHistoryInfo>)ViewState["InitHistoryTabList"]; if (ViewState["RequestedLevelAsc"] == null || (bool)ViewState["RequestedLevelAsc"] == true)
{
var sorted = from c in originalList orderby c.RequestedSecurityLevel ascending select c;
foreach (var info in sorted)
{
sortedList.Add(info);
}
ViewState["RequestedLevelAsc"] = false; }
else
{
var sorted = from c in originalList orderby c.RequestedSecurityLevel descending select c;
foreach (var info in sorted)
{
sortedList.Add(info);
} ViewState["RequestedLevelAsc"] = true;
} grdHistoryRequest.DataSource = sortedList;
grdHistoryRequest.DataBind();
ViewState["InitHistoryTabList"] = sortedList; }
真正困惑我的是,如果我把页面上的LinkButton删掉,排序会 米!有!效!
百思不得姐,姐百思不得。。。。
Gridview 多重表头 (二)的更多相关文章
- Gridview 多重表头 (一)
今天看到一个人每个月更新博客,结果七年后改行去卖土特产...感慨良多... 虽然我也想去开餐厅~~ 今天需求里有一个多重表头,感觉比较奇特,特意留下记录,以防我的大脑被艾滋海默攻占~~没有女主的命,不 ...
- GridView合并表头多重表头
后台代码: using System; using System.Data; using System.Configuration; using System.Web; using System.We ...
- GridView七十二绝技-大全(收藏版)(转至别人博客)
快速预览:GridView无代码分页排序GridView选中,编辑,取消,删除GridView正反双向排序GridView和下拉菜单DropDownList结合GridView和CheckBox结合鼠 ...
- Silverlight多重表头实现
效果: 实现主要逻辑:通过动态拼接XML生成表头样式,绑定到列上. 主要是动态拼接XML时要仔细核对对应的占位行,具体可以看代码,注释很详细 两个类一个接口 NTree<T>:定义表头树形 ...
- GridView合并表头、多重表头(转)
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { switch (e.Row.RowType) ...
- gridview自定义表头
gridview为我们提供了丰富的接口,用于满足自定义需求. 通常asp:gridview会根据绑定的列Columns自动生成表头,展现在前台元素. 序号 类别 有时候需要复杂一些的表头. 序号 类别 ...
- GridView 自定义表头
//修改表头 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { switch (e.Row.Ro ...
- 从头学起android<GridView网格视图.二十八.>
GridView基于组件的网络格所有的形式的组分的,例如:当制作专辑,所有的图片将在相同的尺寸在不同的显示格在孩子,是否能够依靠这个组件完成.此组件的继承结构参见例如下面: java.lang.Obj ...
- Gridview 重建表头/单击单元格弹出对话框/改变单元格背景色
整理工作~ 完整的代码在GitHub上, 路径: 项目背景:追踪某个issue,并且记录每天的状态. 要求:1.点击日期就能更改,并且用颜色标志不同的状态 2.增加按钮可关闭issue 3.布局要求日 ...
随机推荐
- [Spring入门学习笔记][创建网站URL]
设计网站的URL 现代的Web站点都会设计一套拥有明确意义,方便用户记忆的URL,不论是域名还是路径,以天码营为例: http://tianmaying.com/courses表示网站下所有的课程列表 ...
- stm32之通用定时器TIM
STM32系列的CPU,有多达8个定时器: 1.其中TMI1和TIM8是能够产生三对PWM互补输出的高级定时器,常用于三相电机的驱动:它们的时钟有APB2的输出产生: 2.其它6个为普通定时器,时钟由 ...
- Android 4.4前后版本读取图库图片和拍照完美解决方案
转载:http://blog.csdn.net/zbjdsbj/article/details/42387551 4.3或以下,选了图片之后,根据Uri来做处理,很多帖子都有了,我就不详细说了. 主要 ...
- 2016 ACM/ICPC Asia Regional Shenyang Online
I:QSC and Master 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5900 题意: 给出n对数keyi,vali表示当前这对数的键值和权值 ...
- System.Speech.Synthesis 添加暂停、继续功能
为了方便调用暂停.继续的方法.要将speech的功能写成一个类.直接附上代码: using System; using System.Collections.Generic; using System ...
- Could not load file or assembly 'Oracle.DataAccess' or one of its dependencies. An attempt was made to load a program with an incorrect format.
I have installed a Web application on IIS 7.0 windows server 2008 R2 64 bit OS I am refering a oracl ...
- TransactionScrope
测这个东西其实是由生产环境数据库报错,ORA-14450错误. 测试结果是: 1)使用transactionscrope时,数据库连接打开需在scrope内打开: 2)TransactionScope ...
- 空合并操作符??(C#)
??二元操作符在对first??second求值时,大致会经历以下步骤: 1)对first进行求值: 2)如果结果非空,则该结果就是整个表达式的结果: 3)否则求second的值,其结果作为整个表达式 ...
- Web 1三级联动 下拉框 2添加修改删除 弹框
Web 三级联动 下拉框 using System; using System.Collections.Generic; using System.Linq; using System.Web; u ...
- MFC串口通信
1.串口的操作可以有两种操作方式:同步操作方式和重叠操作方式(又称为异步操作方式). 同步操作时,API函数会阻塞直到操作完成以后才能返回(在多线程方式中,虽然不会阻塞主线程,但是仍然会阻塞监听线程) ...