DataGridView White Space after the last column header

I'm trying to imitate what every other table view does with the DataGridView control, but I can't figure out that the headers are correct.

I need an empty title to the right of all the titles, which does not move and is not really a title. Is there a way to draw the default caption at the top?

Basically, this is my problem:

Problemm

+6
header paint datagridview
source share
4 answers

try it

Dim dt As New DataTable() dt.Columns.Add("a") dt.Columns.Add("b") dt.Rows.Add(dt.NewRow()) dt.Rows.Add(dt.NewRow()) dt.Rows.Add(dt.NewRow()) dt.Rows.Add(dt.NewRow()) dt.Rows.Add(dt.NewRow()) dt.Rows.Add(dt.NewRow()) dt.Columns.Add(" ") dt.AcceptChanges() DataGridView1.DataSource = dt DataGridView1.AutoSize = True DataGridView1.Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill DataGridView1.Columns(2).Resizable = DataGridViewTriState.False 
+6
source share

Until it answers your question, an alternative would be to set AutoSizeColumnsMode to Fill .

0
source share

After adding all the columns, you can add an additional column by setting the following properties:

  AutoSizeMode = Fill; HeaderText = "" ReadOnly = true; SortMode = NotSortable; 

handle the gridView CellPainting event for this particular column, gridView CellPainting event drawing:

  private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.RowIndex > -1 && e.ColumnIndex == dataGridView1.Columns.Count - 1) { e.AdvancedBorderStyle.All = DataGridViewAdvancedCellBorderStyle.None; e.PaintBackground(e.ClipBounds, false); e.Handled = true; } } 

and you get what you want.

0
source share

IMO, the best (and most efficient) way to do this is to create an extra column at the end so that it can β€œeat” (or β€œ tackle,”) a space that is not occupied by other columns. The way to do this is to set the AutoSizeMode property to .

Here is a 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 a grid with an additional empty column. We must set the column to the desired properties:

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

In addition, as najameddine explained, you can set the following properties:

ReadOnly = true;

SortMode = NotSortable;

Essentially, you create an empty column that takes up empty space.

PS I just noticed that suraidhamarai has a very similar code example, but mine is in C #, however the concept remains the same.

0
source share

All Articles