DataGridView White Space After Last Column Header

前端 未结 4 777
时光说笑
时光说笑 2021-02-09 20:28

I\'m trying to mimic what every other tabular view does with the DataGridView control, but I can\'t seem to get the headers correct.

I want a blank header to the right o

4条回答
  •  伪装坚强ぢ
    2021-02-09 21:09

    IMO, the best (and most efficient) way to do this is by creating an extra column at the end, to allow it to "eat up" (or "take up") the space that isn't occupied by other columns. The way to do this is to set the AutoSizeMode property to Fill.

    Here is some sample code:

    DataGridView grid = new DataGridView();
    DataTable data = new DataTable();
    
    //add columns, rows, etc. to DataTable data
    data.Columns.Add("This is the first column."); data.Rows.Add(data.NewRow());
    //etc.
    
    //Add EXTRA column:
    
    data.Columns.Add(""); //blank header
    
    //Save changes
    
    data.AcceptChanges();
    
    //Set datasource
    grid.DataSource = data;
    

    Now, you have the grid, with an extra blank column. We have to set the column to the right properties:

    data.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; //Sets AutoSizeMode to fill, as explained above, for 2nd column
    

    Also, as najameddine explained, you might want to set the following properties:

    ReadOnly = true;

    SortMode = NotSortable;

    Essentially, you're creating a blank column that takes up the empty space.

    P.S. I just noticed that surajitkhamrai has a very similar code sample, but mine is in C# - however, the concept stays the same.

提交回复
热议问题