How get focus in new row in DataGrid?

B

Bill Todd

I have a WinForms DataGrid bound to a DataView. I insert a new row by
calling CurrencyManager.AddNew. However, the user cannot begin
entering data until he/she clicks in the first cell of the new row.
How can I set focus to the first cell of the new row in code so the
user can start typing?

Note that I have tried stting the DataGrid.CurrentCell property and
that does _not_ work.

Thanks.
 
R

Randy A. Ynchausti

Bill,
I have a WinForms DataGrid bound to a DataView. I insert a new row by
calling CurrencyManager.AddNew. However, the user cannot begin
entering data until he/she clicks in the first cell of the new row.
How can I set focus to the first cell of the new row in code so the
user can start typing?

Note that I have tried stting the DataGrid.CurrentCell property and
that does _not_ work.

Try using the SelectedIndex member.

Regards,

Randy
 
K

Kevin Yu [MSFT]

Hi Bill,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to set focus to the first cell
of the newly added row in the DataGrid. If there is any misunderstanding,
please feel free to let me know.

Thanks for Randy's quick response. However, the SelectedIndex property is
only available in the web control. I think Bill is working on a WinForm
DataGrid. You might have ignored it.

As far as I know, when a new row is added to the collection, the current
row will be set to the newly added one. Now we can just call
dataGrid.Focus() method to set focus to the whole grid and the cursor will
be on the first cell of the blank row.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
B

Bill Todd

As far as I know, when a new row is added to the collection, the current
row will be set to the newly added one. Now we can just call
dataGrid.Focus() method to set focus to the whole grid and the cursor will
be on the first cell of the blank row.

Thanks. I tried that. It does not work. Any other ideas? T
 
K

Kevin Yu [MSFT]

Hi Bill,

It works well on my computer, when the DataGrid got focus, it will set the
current cell to the first cell of the newly added row, if you
usedataGrid.Focus method right after AddNew. Is my understanding on your
question correct?

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
B

Bill Todd

My problem is that after calling AddNew the Focus both the DataGrid
and the CurrencyManager think that the current cell is the first
column in the new row. The CurrencyManager.Position property and the
DataGrid.CurrentCell.RowNumber and ColumnNumber properties all confirm
this.

The problem is that if I start typing the characters I type do not
appear in the new row (or anywhere else). Before I can type anything
into the new row through the UI I have to click the cel with the
mouse.

Are you able to enter data into the new row from the keyboard after
calling AddNew and Focus?
 
K

Kevin Yu [MSFT]

Hi Bill,

Yes, I was able to enter data into the new row from the keyboard after
calling AddNew and Focus. Please try to check if the AddNew and Focus
methods are called at the end of the function, so that no further operation
is done after these two calls, and we can ensure to put the focus in the
first cell of the newly added row.

If that still doesn't work, could you please send me a small package that
can reproduce the problem via email? I'd be happy to debug it. Removing
'online' from this no spam email is my real email address.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
K

Kevin Yu [MSFT]

Hi Bill,

I'd like to know if this issue has been resolved yet. Is there anything
that I can help. I'm still monitoring on it. If you have any questions,
please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

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