Moving the column of RenderTable

为君一笑 提交于 2019-11-28 02:17:00
Mohita

We regret to mention but there isn't any function that can allow the moving of column of RenderTable to a specified index since the Cols of C1PrintDocument is ReadOnly.

I've done this by creating a new class from System.Web.UI.WebControls.GridView. I override CreateColumns which is used to return an array of the column objects in order. I read a cookie from the page (this allows me to change the columns via a cookie on the page) and create a new column array based on the cookie. This cookie is just a string of the column names in the order required with a | separator. I had another column picker page that would set this cookie. If you don't need to change the columns with a cookie this is not needed -- you could read / create this string from a database or configuration file. I believe the code is well commented and clear -- one note, our application has a requirement to include hidden columns, so I add those to the end of the column list before I return the array.

using System.Collections;
using System.Linq;
using System.Web.UI.WebControls;

public class ChangeColumnGridView : System.Web.UI.WebControls.GridView
{
  protected override ICollection CreateColumns(PagedDataSource dataSource, bool useDataSource)
  {
    // Get the needful from the base class
    var baseColList = base.CreateColumns(dataSource, useDataSource);
    var inColList = baseColList.OfType<object>();

    // Get our column order 
    string columnOrder;

    if (Page.Request.Cookies["colOrder"] != null)
      columnOrder = Page.Request.Cookies["colOrder"].Value;
    else
      return baseColList;

    // change it to an array
    string[] columnOrderA = columnOrder.Split(new char[] { '|' });

    // this is where we will put our results
    ArrayList newColumnList = new ArrayList();

    // look for each name in the list and add when we find it.
    foreach (string name in columnOrderA)
    {
      var found = inColList.Where((c) => c.ToString() == name).FirstOrDefault();

      if (found != null)
        newColumnList.Add(found);
    }

    // look for non-visible items in the list and add them if we don't already have them.
    foreach (var a in inColList)
    {
      if (((System.Web.UI.WebControls.DataControlField)a).Visible == false)
      {
        var found = newColumnList.Cast<object>().Where((c) => c.ToString() == a.ToString()).FirstOrDefault();
        if (found == null)
          newColumnList.Add(a);
      }
    }

    return newColumnList;
  }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!