Moving a RenderTable Column

I have renderTable, and I add rows and columns to the table as follows:

RenderTable renderTable = new RenderTable(); DataTable dt = GetData(); foreach (DataRow row in dt.Rows) { var header = renderTable.Rows[renderTable.Rows.Count]; header[0].Text = "Column 1"; header[1].Text = "Column 2"; header[2].Text = "Column 3"; header[1].Text = "Column 4"; var data = renderTable.Rows[renderTable.Rows.Count]; data [0].Text = row["col1"].ToString(); // 10 data [1].Text = row["col2"].ToString(); // 11 data [2].Text = row["col3"].ToString(); // 12 data [3].Text = row["col4"].ToString(); // 13 } 

This works fine and the table renders like folllows -

 Column 1 Column2 Column3 Column4 10 11 12 13 

My requirement: now I want to move column 4 to another place, for example, to 2nd place. (this place may vary depending on condition)

 Column 1 Column4 Column2 Column3 10 13 11 12 

I tried the Insert method, but it does not work for me, since the insert index may change. Is there any rendering table function to move a column to a specified index.

Please offer any alternative, if any.

+1
source share
2 answers

We regret the mention, but there is no function that can allow the RenderTable column to move to the specified index, since Cols from C1PrintDocument is ReadOnly.

+1
source

I did this by creating a new class from System.Web.UI.WebControls.GridView. I override CreateColumns, which is used to return an array of column objects in order. I read the cookie from the page (this allows me to change the columns through the cookie on the page) and create a new array of columns based on the cookie. This cookie is simply a string of column names in the order required by the separator | . I had another column selection page that would set this cookie. If you do not need to change the columns using a cookie, this is not required - you can read / create this row from the database or configuration file. I believe the code is well-commented and understandable - one note: our application has a requirement to include hidden columns, so I add them to the end of the column list before returning 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; } } 
0
source

Source: https://habr.com/ru/post/906689/


All Articles