dataGrid分组

GridView的分组

public partial class SystemManagement_RoleDetailView : BasePage
{

  private bool _SortedFlag = false;
    private int _SortColumnIndex = -1;
    private string _SortColumnHeader = ""; 

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            PopulateGrid("BizDomainDisplayName", SortDirection.Ascending);  

         ResolveGroupingData("BizDomainDisplayName");

  }

    private void ResolveGroupingData(String SortExpression)
    {
        foreach (DataControlField field in grid1.Columns)
        {
            if (field.SortExpression == SortExpression)
            {
                _SortedFlag = true;
                _SortColumnIndex = grid1.Columns.IndexOf(field);
                _SortColumnHeader = field.HeaderText;
                break;
            }

        }
    }

    private void PopulateGrid(string sortfield, SortDirection SortDirection)
    {
        try
        {
            DataView dw = GetData();
            String sortd = String.Empty;
            if (SortDirection == SortDirection.Ascending)
            {
                sortd = "ASC";
            }
            else
            {
                sortd = "DESC";
            }

            if (dw != null)
            {
                dw.Sort = sortfield + " " + sortd;
            }
            grid1.DataSource = dw;
            grid1.DataBind();
            grid1.Columns[0].Visible = false;
            grid1.Columns[1].Visible = false;
            grid1.Columns[2].Visible = false;
        }
        catch(Exception ex)
        {
            LogUtil.Log.Error(this, ex);
        }
    }

 

//关键的一个重写函数
    protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {

        if (_SortedFlag)
        {

            Table table = (Table)grid1.Controls[0];
            Hashtable htLookUp = new Hashtable();
            foreach (GridViewRow row in grid1.Rows)
            {
                int realIndex = table.Rows.GetRowIndex(row);
                string text = row.Cells[_SortColumnIndex].Text;
                if (!htLookUp.ContainsKey(text))
                {
                    htLookUp.Add(text, null);
                    GridViewRow newHeaderRow = new GridViewRow(realIndex, realIndex, DataControlRowType.DataRow, DataControlRowState.Normal);
                    TableCell newCell = new TableCell();
                    newHeaderRow.Cells.Add(newCell);
                    newCell.ColumnSpan = grid1.Columns.Count;
                    newCell.BackColor = Color.FromArgb(249, 249, 235);
                    newCell.Attributes.Add("ForeColor","#0045A4");
                    newCell.Font.Bold = true;
                    newCell.Text = _SortColumnHeader + " : " + text;
                    table.Controls.AddAt(realIndex, newHeaderRow);
                }
            }

        }
        base.Render(writer);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值