Updated ReshuffleColumns Method


Here is an updated version of 'ReshuffleColumns' method of article DataGrid Customization by Mahesh Chand. To understand the usefulness of this method, you must have to look at DataGrid Customization article. This methods maintains all the properties of the original TableStyle.

public void ReshuffleColumns(DataGridTableStyle tableStyle, int col1, int col2)
{
// init vars
int counter = tableStyle.GridColumnStyles.Count;
string mapName = tableStyle.MappingName;
DataGrid grid = tableStyle.DataGrid;
// Store original columns
ArrayList colStyles = new ArrayList();
for (int i=0; i<counter; i++)
{
colStyles.Add(tableStyle.GridColumnStyles[i]);
}
// Delete columns from tabelstyle
while (tableStyle.GridColumnStyles.Count != 0)
tableStyle.GridColumnStyles.RemoveAt(tableStyle.GridColumnStyles.Count-1);
// Copy columns back to the tablestyle and reorder columns doing so
for(int i=0; i<counter; i++)
{
if(i == col1)
tableStyle.GridColumnStyles.Add((DataGridColumnStyle)colStyles[col2]);
else if(i == col2)
tableStyle.GridColumnStyles.Add((DataGridColumnStyle)colStyles[col1]);
else
tableStyle.GridColumnStyles.Add((DataGridColumnStyle)colStyles[i]);
}
}


Similar Articles