简体   繁体   中英

In datagridview how to 2 column that will freeze?

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?

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;

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.

private void Button4_Click(object sender, System.EventArgs e)
{

    FreezeBand(dataGridView.Rows[0]);
}

private void Button5_Click(object sender, System.EventArgs e)
{

    FreezeBand(dataGridView.Columns[1]);
}

private static void FreezeBand(DataGridViewBand band)
{
    band.Frozen = true;
    DataGridViewCellStyle style = new DataGridViewCellStyle();
    style.BackColor = Color.WhiteSmoke;
    band.DefaultCellStyle = style;
}
dataGridView1.Columns["columnname"].Frozen = true;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM