Home Forums WinForms controls Xceed Grid for WinForms Freezing more than 2 columns using C# Reply To: Freezing more than 2 columns using C#

User (Old forums)
Member
Post count: 23064

I’ve tried something, and I have a solution that almost works. Try this out:<code>public class ColumnFreezer
{
private Xceed.Grid.Column[] columns_;
public ColumnFreezer(params Xceed.Grid.Column[] columns)
{
columns_ = columns;
if (columns != null && columns.Length > 0)
{
Xceed.Grid.GridControl grid = columns[0].GridControl;
if (grid != null) grid.FirstVisibleColumnChanged += new EventHandler(grid_FirstVisibleColumnChanged);
}
}

private void grid_FirstVisibleColumnChanged(object sender, EventArgs e)
{
Xceed.Grid.GridControl grid = sender as Xceed.Grid.GridControl;
if (grid != null)
{
Xceed.Grid.Column firstColumn = grid.FirstVisibleColumn;
if (firstColumn != null)// && firstColumn != columns_[0])
{
Xceed.Grid.Column[] swapColumns = new Xceed.Grid.Column[columns_.Length];

int firstIndex = firstColumn.VisibleIndex;
int count = grid.Columns.Count;
for(int i=0; i<swapColumns.Length; i++)
{
for(int j=0; j<count; j++)
{
if (grid.Columns[j].VisibleIndex == firstIndex + i)
{
swapColumns[i] = grid.Columns[j];
break;
}
}
}

for(int i=0; i<columns_.Length; i++)
{
int index = columns_[i].VisibleIndex;
columns_[i].VisibleIndex = swapColumns[i].VisibleIndex;
swapColumns[i].VisibleIndex = index;
}
}
}
}
}</code>

Imported from legacy forums. Posted by Tommy (had 161 views)