Windows Form: change dataGridView for first cell?

In short, I have this dataGridView, and I want cell [0,0] to be a cell in the lower left corner of the grid, and not in the upper left corner of the grid, as is done by default.

For example, visually, if I do something like:

dataGridView1[0, 0].Value = "a"; 

I get this (unfortunately, not enough reputation for posting photos)

but I would like the “a” to appear, following the same instruction, in the blue highlighted slot and doing things like adding a line to be added at the top of the grid.

Thanks a lot in advance and welcome

+5
source share
2 answers

Create one class as follows:

 public class MyDataGridView : DataGridView { public new DataGridViewCell this[int col, int invertRow] { get { int recordCount = this.RowCount - (this.AllowUserToAddRows ? 2 : 1); return this.Rows[recordCount - invertRow].Cells[col]; } set { int recordCount = this.RowCount - (this.AllowUserToAddRows ? 2 : 1); this.Rows[recordCount - invertRow].Cells[col] = value; } } } 

and name it like this:

 dataGridView1[0, 0].Value = "a"; 

or if you want to set or get the first cell in the upper left corner of the grid, then you can use the FirstDisplayedCell property.

MSDN: Gets or sets the first cell currently displayed in the DataGridView; usually this cell is in the upper left corner.

For instance:

 dataGridView1.FirstDisplayedCell.Value = "a"; 
+4
source

There is no own way to do what you want without extending the class, but you can use extension methods that will invert the row index for you:

 public static DataGridViewCell FromLowerLeft(this DataGridView dgv, int columnIndex, int invertedRowIndex) { return dgv[columnIndex, dgv.RowCount - invertedRowIndex]; } 

It can be used as

 dataGridView.FromLowerLeft(0,0).Value = "a"; 
+4
source

All Articles