2

I have a datagridview with the columns

col A | col B | ... | col M | col N 

Now I want to freeze col N on the right side, so that when the user scrolls the datagridview horizontally the columns A,..., M can be scrolled horizontally, but col N remains frozen.

Now I have tried setting the Frozen attribute for col N but then all the columns to the left of the frozen column are also frozen, which I do not want. The best that I have been able to come up with is to reverse the columns of the DataTable bound to the DataGridView so that it now has the order

col N | col M | ... | col B | col A

and then draw the DataGridView from RightToLeft so that the columns are reversed again and then shown as

col A | col B | ... | col M | col N 

Another solution that I tried is to extract the rightmost columns that have to be frozen and place them in a different DataGridView on the right side and the remaining columns in the original DataGridView and then synchronize the vertical scroll for both of them. Now my question is are there any better ways to do this, if not which of the above should I prefer?

or in other words :

How can I freeze columns on the right side of a datagridview without freezing the other columns ?

Joe Vi
  • 473
  • 4
  • 10
  • What is the thing you don't like in the current solution? I can see that the column of row headers is right-aligned and that may be what you don't like? – King King Jul 29 '13 at 10:34
  • that and the overhead of reversing the columns in the datatable in both cases, i.e. from the datatable to the gridview and from the gridview to the datatable for any manually entered values by the user, because although the user thinks he is entering a value in col A , he is actually entering it in col N. – Joe Vi Jul 29 '13 at 10:42

2 Answers2

1

joe, in c# DataGridView yo can freeze the column in the middle of the table, only on the start of it, just like in excel. It makes sense, because if we freeze the middle column then we don't know how it should react in the sides. will we need two scroll bars for the different sides?

from here you can see that it is the c# wanted behavior:

When a column is frozen, all the columns to its left (or to its right in right-to-left languages) are frozen as well. The frozen and unfrozen columns form two groups. If column repositioning is enabled by setting the AllowUserToOrderColumns property to true, the user cannot drag a column from one group to the other.

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
  • I agree, but are there any workarounds other than the ones that I suggested? – Joe Vi Jul 30 '13 at 08:23
  • i've tried a couple of years ago and ended up giving up. there is no workaround unless you build your own control, i mean the entire thing including paint(). that's not worth the time... – No Idea For Name Jul 30 '13 at 08:27
0

you can try this:

 var cols = dcols.ToArray();//dcols is the DataGridViewColumn List wait to add to DataGridView
    if (cols.Last().Frozen)
    {
        _this.RightToLeft = RightToLeft.Yes;
        cols = cols.Reverse().ToArray();
    } 
    _this.Columns.AddRange(cols);
    //Note that DataGridView does not allow freezing on both sides at the same time, or freezing some columns in the middle.

You will have a problem. The scrollbar will be positioned to the far right. Then, you can try the code below:

//OnDataBindingComplete
        if (this.Columns.Count > 0)
            this.FirstDisplayedScrollingColumnIndex = this.Columns.Count - 1;
sgf
  • 11
  • 2