set focus to cell in datagrid

Heike

Regular
Joined
Jun 5, 2002
Messages
56
I have a datagrid containing two columns. The first column is not editable the second is editable. What I want is, that after loading my form the datagrid has the focus and I can start to enter my data without clicking anywhere.

This is how I tried it:
Visual Basic:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal msg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Boolean

'[.....]

Dim dgc As New DataGridCell(0, 1)   
Dim dgtb As DataGridTextBoxColumn   

'do something

Me.grdData.Focus()
Me.grdData.CurrentCell = dgc

dgtb = CType(Me.grdData.TableStyles(IBS_Vorschau.objDataSetVorschau.strTableName).GridColumnStyles(1), DataGridTextBoxColumn)
dgtb.TextBox.Focus()
dgtb.TextBox.Select()

SendMessage(dgtb.TextBox.Handle, 512, vbNull, vbNull)
SendMessage(dgtb.TextBox.Handle, 513, vbNull, vbNull)

At least this lets the user move through the cells using the arrow keys. Enter data right from the beginning isn't possible and another problem is, that I have to click twice on my toolbarbuttons.

Any idea?
 
I had the exact same situation. I have a small page that loads with a datagrid on it, which I wanted to always default to the first textbox in the footer for data entry. While I haven't gotten the ENTER key to function to process the entry command from anywhere on the page, I do have code that should help you.

I posted it on the www.asp.net forums..here is the link

http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=206488

Hope this helps.

-Kirk
 
Back
Top