views:

130

answers:

1

How do you set focus to a specific control in a datagridview row after clicking the edit button? I'm able to do it for a new row when the grid is binding, but not for an existing row. The control doesn't seem to exist yet.

'This doesn't work (existing row)

Protected Sub gvDays_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) Handles gvDays.RowEditing
        Try
            gvDays.EditIndex = e.NewEditIndex
            gvDays.Rows(e.NewEditIndex).FindControl("txtDayText").Focus()
        Catch ex As Exception
            Helper.WriteException(ex)
        End Try
    End Sub


'This does work for a newly bound row

Private Sub gvDays_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvDays.RowDataBound
        If e.Row.RowState = DataControlRowState.Edit Then
            e.Row.Cells(3).Controls(0).Focus()
        End If
    End Sub
+1  A: 

gvDays_RowDataBound should work, the problem is you are looking at e.Row.RowState using the = operator, but RowState is a bitflag

try this

Private Sub gvDays_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvDays.RowDataBound
        If (e.Row.RowState And DataControlRowState.Edit) = DataControlRowState.Edit Then
            e.Row.Cells(3).Controls(0).Focus()
        End If
End Sub
Pratap .R