0
votes

I have a DataGridView with properties DataSource = datatable() and readonly = false. readonly must be false since there are other columns that can be editable. How do I make all the columns in DataSource read only (not editable)?

The code is as follows:

type = new DataGridViewComboBoxColumn();
        table= new DataGridView
                         {
                             DataSource = datatable(), // this returns a DataTable object
                             AllowUserToAddRows = false,
                             AllowUserToDeleteRows = false,
                             RowHeadersVisible = false,
                             MultiSelect = false,
                             Name = "AgentTable",
                             AutoSize = true,
                             ReadOnly = false,
                         };
        table.Columns.Add(CreateStartButton());        
        type.Items.Add(" some table");
        type.ReadOnly = false;
        table.Columns.Add(type);

EDIT: the datagridview will contain 4 columns.

  • First column, each cell is a button (readonly doesnt matter)
  • second column, each cell is a drop down box (readonly is false)
  • third and fourth columns are created as DataTable object so (readonly must be true)

so my question is how to make the third and forth column read-only?

1
You mean: the columns in the data table should be readonly and the added columns editable?Gert Arnold
@GertArnold: please see edit question aboveCarlitos Overflow

1 Answers

0
votes

It should be easy to iterate the columns after you create them all. I'd use something like this, but I'm calling it pseudocode because I don't know if DataColumn is the correct data type:

foreach(DataColumn col in table.Columns) {
    col.ReadOnly = true;
}