DataGrid自动生成列 控制列头显示

情景:
1. DataGrid的列是自动生成的。
2. DataGrid支持排序。
要求:点击某列的列头时,根据该列对应的字段排序。升序/降序在字段后面加上“向上/向下”箭头。

由于列是自动生成,所以DataGrid.Columns集合为空。无法遍历Columns的HeaderText。

有个办法:在DataBound中绑定行列头时设置单元格中的内容。

None.gifprivate void dgMain_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif  
//dot.gif
InBlock.gif
if ( e.Item.ItemType==ListItemType.Header)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
InBlock.gif                
for(int i=0;i<e.Item.Cells.Count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    e.Item.Cells[i].Attributes.Add(
"class","GridHead");
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif                    
排序的上下箭头#region 排序的上下箭头
InBlock.gif                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if(i > 0//不处理第0列
ExpandedSubBlockStart.gifContractedSubBlock.gif
                        dot.gif{
InBlock.gif                            LinkButton lbHeader 
= (LinkButton)e.Item.Cells[i].Controls[0];
InBlock.gif                            
string currentColumnName = lbHeader.Text;                            
InBlock.gif                            
string sortExpression = string.Empty;
InBlock.gif                            
string sortDirection = string.Empty;
InBlock.gif                            
if(dgMain.Attributes["SortExpression"!= null)                            
InBlock.gif                                sortExpression 
= dgMain.Attributes["SortExpression"].ToString();                            
InBlock.gif                            
if(dgMain.Attributes["SortDirection"!= null)
InBlock.gif                                sortDirection 
= dgMain.Attributes["SortDirection"].ToString();
InBlock.gif                            
if(sortExpression == currentColumnName)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                
if(sortDirection == "desc")
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
dot.gif{
InBlock.gif                                    lbHeader.Text 
= currentColumnName + "";
ExpandedSubBlockEnd.gif                                }

InBlock.gif                                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
dot.gif{
InBlock.gif                                    lbHeader.Text 
= currentColumnName + "";
ExpandedSubBlockEnd.gif                                }

ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{}
ExpandedSubBlockEnd.gif                    
#endregion

ExpandedSubBlockEnd.gif                }

InBlock.gif
ExpandedSubBlockEnd.gif}

需提醒的是:
可排序列中的列头单元格中的内容是LinkButton类型。对这种列头用e.Item.Cells[0].Text是取不到内容的。

另外:在页面中Page_Load或者其它地方,用DataGrid.Items取到的是DataGrid的普通内容,即行内容,而不包括Header和Footer。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值