Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In datagridview how to 2 column that will freeze?

Tags:

c#

winforms

I have datagridview in my C# program

and when I move scroll-bar to left, I want that the 2 right columns

will freeze and don't move.

how to do it?

like image 580
Gold Avatar asked Dec 05 '22 02:12

Gold


2 Answers

Theres a properties in the columns section of the datagridview to freeze the columns.

Go to your datagridview --> Columns --> (Column you want frozen) --> Frozen = True

EDIT:

After testing it seems that it will only freeze the columns on the left of the datagrid not the right.

EDIT 2:

To get it to freeze the columns on the right enable the "RightToLeft" property on the datagrid. It reverses the order the columns are drawn and allows the rightmost columns to be frozen.

dataGridView1.Columns["columnname"].Frozen = true;
dataGridView1.RightToLeft = Enabled;
like image 78
Gage Avatar answered Jan 17 '23 03:01

Gage


You might use one of the DataGridViewElementStates enumeration values.

Either use an index:

dataGridView1.Columns[0].Frozen = true;

or use the column name:

dataGridView1.Columns["columnName"].Frozen = true;

You may also use the DataGridViewColumnCollection.GetFirstColumn() method:

dataGridView1.Columns.GetFirstColumn(DataGridViewElementStates.Frozen);

I would personnaly go with the index, since you want the two first columns to freeze. Then, when you want to change these frozen columns, you will only have to change their index in design.

As for making the two columns on the right frozen, I would simply bring them to the right, it is a more ergonomical way to do it, as most of the time, we read from left to right.

like image 34
Will Marcouiller Avatar answered Jan 17 '23 03:01

Will Marcouiller