DataGridView.CurrentCell (Set)

  • Thread starter Thread starter Michel Walsh
  • Start date Start date
M

Michel Walsh

Someone has an example, C#, about how to set the cell(int row, int col) as
the current cell for a DataGridView. The example, in the help file, about
DataGrid.CurrentCell Property (Set)

myGrid.CurrentCell = new DataGridCell(1,1);


does not work, fro a DataGridView, since we cannot create a DataGridView
Cell while specifying only its row and column.


Vanderghast, Access MVP
 
Sorry for the bandwidth, just found that I just have to point to that said
cell, after all... doh. No need to 'create' one, as the example from
DataGrid leads us to think. The following works:


dataGridView1.CurrentCell = dataGridView1.Rows[iRow].Cells[iCol];




Vanderghast, Access MVP
 
Michael,

There are more succinct ways of doing this:

dataGridView1.CurrentCellAddress = new Point(1, 1);

Or, if you wish:

dataGridView1.CurrentCell = dataGridView1[1, 1];

Both should have the same effect, although the top example will help
prevent unsharing of rows, which is good when you are using the grid in
virtual mode.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Michel Walsh said:
Sorry for the bandwidth, just found that I just have to point to that said
cell, after all... doh. No need to 'create' one, as the example from
DataGrid leads us to think. The following works:


dataGridView1.CurrentCell = dataGridView1.Rows[iRow].Cells[iCol];




Vanderghast, Access MVP



Michel Walsh said:
Someone has an example, C#, about how to set the cell(int row, int col)
as the current cell for a DataGridView. The example, in the help file,
about DataGrid.CurrentCell Property (Set)

myGrid.CurrentCell = new DataGridCell(1,1);


does not work, fro a DataGridView, since we cannot create a DataGridView
Cell while specifying only its row and column.


Vanderghast, Access MVP
 
If I can ask you one more question.


While

dataGridView1.CurrentCell = dataGridView1[1, 1];

is fine for me, if I consider dataGridView[1,1] is a reference (a pointer)
to an existing cell, and then CurrentCell property just 'set the focus' on
that 'object reference' it got as its 'value', I fail to see how:


dataGridView1.CurrentCellAddress = new Point(1, 1);


could even work, since

new Point(1, 1)

is also a reference, but totally unrelated to any cell-references
(cell-pointers) owned by the dataGridView object. With this, in
consideration, can you explain the inner-working of


dataGridView1.CurrentCellAddress = new Point(1, 1);




Thanks for your time
Vanderghast, Access MVP
 
dataGridView1.CurrentCellAddress = new Point(1, 1);

causes error
'System.Windows.Forms.DataGridView.CurrentCellAddress' cannot be assigned
to -- it is read only


So how to use it ?

Andrus.
 
Michel,

As you and Andrus pointed out, you the CurrentCellAddress property is
read-only, so you can't set it.

So only the first example I gave is valid.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Michel Walsh said:
If I can ask you one more question.


While

dataGridView1.CurrentCell = dataGridView1[1, 1];

is fine for me, if I consider dataGridView[1,1] is a reference (a pointer)
to an existing cell, and then CurrentCell property just 'set the focus' on
that 'object reference' it got as its 'value', I fail to see how:


dataGridView1.CurrentCellAddress = new Point(1, 1);


could even work, since

new Point(1, 1)

is also a reference, but totally unrelated to any cell-references
(cell-pointers) owned by the dataGridView object. With this, in
consideration, can you explain the inner-working of


dataGridView1.CurrentCellAddress = new Point(1, 1);




Thanks for your time
Vanderghast, Access MVP



Nicholas Paldino said:
Michael,

There are more succinct ways of doing this:

dataGridView1.CurrentCellAddress = new Point(1, 1);

Or, if you wish:

dataGridView1.CurrentCell = dataGridView1[1, 1];

Both should have the same effect, although the top example will help
prevent unsharing of rows, which is good when you are using the grid in
virtual mode.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top