.netwinformsdatagridviewautogeneratecolumn

Forcing AutoGenerateColumns to create DataGridViewTextBoxColumn for Boolean Data Types


I have a lot of read-only data, and I don't like the clickable look and feel of DataGridViewCheckBoxColumns which are unfortunately the default type for rendering Boolean data.

Is there any way, either before or after using AutoGenerateColumns of a DataGridView that I can force it to generate DataGridViewTextBoxColumns that would just print TRUE or FALSE instead of DataGridViewCheckBoxColumns?

Sample of way Data currently is returned

If it is easier to address this problem on the DataTable before setting it as the DataSource, that would be fine too.


Solution

  • If you want to continue using AutoGenerateColumns = True and DataGridViewCheckBoxColumn, you can modify the appearance and behavior to look and act like a read only field.

    Assuming you wanted to target all CheckBoxCells, you can disable them and change their appearance like this:

    For Each row In DataGridView1.Rows.Cast(Of DataGridViewRow)()
        For Each cell In row.Cells.OfType(Of DataGridViewCheckBoxCell)()
            cell.ReadOnly = True
            cell.FlatStyle = FlatStyle.Flat
            cell.Style.ForeColor = Color.DarkSlateGray
        Next
    Next