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

Tommy,

Thank you so much. I finally was able to get back to this issue and figured out what was happening. I just had to add a little bit to what you had provided. I’m posting my resulting code here for reference just in case others can benefit from it.

Thanks,
Bertina

==========

public class ColumnFreezer
{

private Xceed.Grid.Column[] columns_;
private bool bOrdered = false;

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;

// Check whether or not the first n columns are in order
int iFirstIndex = firstColumn.VisibleIndex;

for (int i = 0; i < columns_.Length; i++)
{

if (columns_[i].VisibleIndex == iFirstIndex + i)
{
bOrdered = true;
}
else
{
bOrdered = false;
break;
}
}

if ((firstColumn != null) && (!bOrdered))
{
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;

}

}

// Take the remaining columns that are not visible and set their visible indices
if (bOrdered)
{

int iColCount = columns_.Length;

int i = columns_[iColCount – 1].VisibleIndex;

// Set the Visible Indices for the “first” non-visible columns (to the left of the frozen columns)
for (int j = i; j > 0; j–)
{
if (grid.Columns[j] != columns_[iColCount – 1])
{
grid.Columns[j].VisibleIndex = grid.Columns[j].Index – iColCount;
}
else
{
break;
}
}

// Reorder the remaining columns in order – columns to the right of the frozen columns
int iMax = grid.Columns.Count;
int iMin = columns_[iColCount – 1].VisibleIndex + 1;

for (int iCol = iMin; iCol < iMax; iCol++)
{
grid.Columns[iCol].VisibleIndex = grid.Columns[iCol].Index;
}

}

}

}

Imported from legacy forums. Posted by bml3k (had 5786 views)