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

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #13598 |

    I have contacted support via email and phone with no response to this question. I have seen previous posts where this has been an issue and there were workarounds suggested. Thank you so much for those who have supplied the workarounds. We have tried them and they all seem to work when trying to freeze/fix two or less columns on the Xceed Grid (ver 2.3). However, we need to have 5 columns fixed.

    I also found a downloadable example that successfully freezes 3 columns (we’re getting closer here), however, it was done in VB and a C# version was not made available. Could someone please shed some light on this or point us in the right direction to find useful C# code to freeze 5 columns? This is a very critical part of our application and any help would be greatly appreciated.

    Regards.

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

    User (Old forums)
    Member
    Post count: 23064

    Have you tried this technique?
    <a href=”http://www.xceedsoft.com/Forums/ShowPost.aspx?PostID=2163″>http://www.xceedsoft.com/Forums/ShowPost.aspx?PostID=2163</a&gt;

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

    User (Old forums)
    Member
    Post count: 23064

    Yes I have and it doesn’t work either. I did hear back from Tech Support and they suggested wrapping my code in BeginInit() and EndInit() – which I forgot about and hoped would help – but it didn’t either. 🙁

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

    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)

    User (Old forums)
    Member
    Post count: 23064

    Tommy,

    Thank you so much! This works much better. However, it is sporadic. I will be leaving the office soon for the weekend so I will get back to it on Monday to look and see what tweaks it may need. Here’s what happens (the goal here is to freeze four columns):

    I single-click the right arrow once to scroll and all of the columns shift as if there is no freezing. I single-click the right arrow again and the columns that are supposed to freeze are “back” and frozen where they should be and the columns to the right shifted accordingly. I single-click the right arrow again and the first column that was supposed to be frozen is now on the right side of the grid. By the time I’ve clicked 10 times, the columns freeze on the left, but the columns on the right have re-arranged themselves.

    I’m not sure if I’m making much sense, but hopefully it is a small tweak that is needed.

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

    User (Old forums)
    Member
    Post count: 23064

    Ok. Got it to work to freeze the first four columns (I put the new ColumnFreezer line in the wrong place). Now the freely moving columns are re-arranging themselves. I will keep you posted. Thank you so much!

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

    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)

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.