How to programmatically select a row in a datagrid

P

Parrot

I cannot programmatically select a row in datagridview control. I use the
following instruction to change the selection of a row

dataGridView2.ClearSelection();
dataGridView2.Rows[currentrow].Selected = true;

When I add another entry to the datagrid, I return to this routine and check
for the current row as follows:

DataGridViewRow row = dataGridView2.CurrentRow;
currentrow = row.Index;

However, unless I manually check the row with my mouse, the currentrow
always returns a 0 even though the 2nd row is highlighted. How can I make
the program retain the select status that I set up in the first routine? Or
how do I determine the current row value?
Dave
 
J

jake

Dave,
dataGridView.CurrentCell = dataGridView[<column>, <row>]
will do the trick.
Hope this helps,
jake
 
P

Parrot

Jake;
Thanks so much for your help. Your suggestion worked. What would we do
without these forums? Thanks again.
Dave

jake said:
Dave,
dataGridView.CurrentCell = dataGridView[<column>, <row>]
will do the trick.
Hope this helps,
jake


I cannot programmatically select a row in datagridview control. I use the
following instruction to change the selection of a row

dataGridView2.ClearSelection();
dataGridView2.Rows[currentrow].Selected = true;

When I add another entry to the datagrid, I return to this routine and check
for the current row as follows:

DataGridViewRow row = dataGridView2.CurrentRow;
currentrow = row.Index;

However, unless I manually check the row with my mouse, the currentrow
always returns a 0 even though the 2nd row is highlighted. How can I make
the program retain the select status that I set up in the first routine? Or
how do I determine the current row value?
Dave
 
J

jake

You're welcome.
"What would we do without these forums?" We would spend a lot of
money on gas and phone bills and still get nowhere!
But seriously, glad I can help.
jake


Jake;
Thanks so much for your help. Your suggestion worked. What would we do
without these forums? Thanks again.
Dave

jake said:
Dave,
dataGridView.CurrentCell = dataGridView[<column>, <row>]
will do the trick.
Hope this helps,
jake
I cannot programmatically select a row in datagridview control. I use the
following instruction to change the selection of a row
dataGridView2.ClearSelection();
dataGridView2.Rows[currentrow].Selected = true;
When I add another entry to the datagrid, I return to this routine and check
for the current row as follows:
DataGridViewRow row = dataGridView2.CurrentRow;
currentrow = row.Index;
However, unless I manually check the row with my mouse, the currentrow
always returns a 0 even though the 2nd row is highlighted. How can I make
the program retain the select status that I set up in the first routine? Or
how do I determine the current row value?
Dave
 
K

Kim S

foreach (DataGridViewRow row in this.dataGridView.Rows)
{
// TODO Why can a cell be null, then wrapMode is set to
false, then I select 2. time?
if (row.Cells[0].Value == null)
row.Cells[0].Value = false;

// compare Select checkbox and RMS database value and
RMS DataGridView column
if (((bool)row.Cells[currencolunm].Value) == true && ---
other condsion )
{
--

In Short Cast Select cell to bool + the row condision as you see 2. time you
select the select state can be null and not only true/false why I still not
know.


Kim S.
 

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

Top