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

Farshad Valizade 381 Reputation points
2023-03-10T05:24:47.5933333+00:00

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

1

pic 1 clicked on the lineno column

2

C#
C#
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,307 questions
0 comments No comments
{count} votes

Accepted answer
  1. Alan Farias 745 Reputation points
    2023-03-10T21:39:54.5866667+00:00

    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
    2023-03-15T07:14:53.3+00:00

    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;
                }
            }
    

    RESULT:

    enter image description here

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

    Best regards,

    Elya


    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.