how to prevent datagridview from changing column type after click on header?

Farshad Valizade 381 Reputation points

hi every body

i have a small problem with dgv

i have a dgv in my form and first column type is checkbox . i have a method that loop trough into dgv rows and changes rows that have special value type into textbox and disbale it to prevent user to check it again.

now it works well and doesn't show those cell chheck box but when i click on the any columns all check box appear again.

what should i do?

see the picture for more detail

pic 1 usual dgv


pic 1 clicked on the lineno column


An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,385 questions
0 comments No comments
{count} votes

Accepted answer
  1. Alan Farias 745 Reputation points

    To prevent the checkbox column from appearing again after clicking on another column header in the DataGridView, you can handle the DataGridView's ColumnAdded event and set the column's DataGridViewCheckBoxColumn.SortMode property to DataGridViewColumnSortMode.NotSortable.

    Here is an example code snippet:

    private void dataGridView1_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
        if (e.Column.Index == 0) // Check if this is the checkbox column
            DataGridViewCheckBoxColumn checkBoxColumn = (DataGridViewCheckBoxColumn)e.Column;
            checkBoxColumn.SortMode = DataGridViewColumnSortMode.NotSortable;

    In this code snippet, we are checking if the added column is the checkbox column and then setting its SortMode property to NotSortable. This will prevent the DataGridView from resorting the rows based on the checkbox column when the user clicks on another column header.

    I hope I was able to help! Don't forget to mark my answer as accepted if it was useful to you. Have a great day!

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. YujianYao-MSFT 4,281 Reputation points Microsoft Vendor

    Hi Farshad Valizade,

    You could also consider hiding the cell check box by setting the cell to be read-only.

    Sample Code:

      this.dataGridView1[0, 2].ReadOnly = true;
      private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
                if (e.ColumnIndex == 0 && e.RowIndex == 2)
                    e.PaintBackground(e.ClipBounds, true);
                    e.Handled = true;


    enter image description here

    Of course, this is just a sample, you should modify it according to your needs.

    Best regards,


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.