Setting cell focus in a DataGridView

sfx

Well-known member
Joined
Jan 31, 2006
Messages
46
Programming Experience
Beginner
Hello All,

How do I set the focus to a specific cell in a DataGridView through code?

Thanks,

sfx
 
It would be advisable for you to find DataGridView class in documentation and browse through the members.
VB.NET:
DataGridView1.CurrentCell = DataGridView1.Item(1, 5)
'or
DataGridView1.CurrentCell = DataGridView1.Item("ColumnName", 5)
 
It would be advisable for you to find DataGridView class in documentation and browse through the members.
VB.NET:
DataGridView1.CurrentCell = DataGridView1.Item(1, 5)
'or
DataGridView1.CurrentCell = DataGridView1.Item("ColumnName", 5)

It doesn't seem as easy as that. I have the following code:

VB.NET:
    Dim cell As DataGridViewCell = incidentsDataGridView.Item(e.ColumnIndex, e.RowIndex)
    Dim column As DataGridViewColumn = incidentsDataGridView.Columns(e.ColumnIndex)
    Dim a As Object

    If cell.Value.ToString.Trim & "" <> "" Then
        Try
            a = CType(cell.Value, Date)
        Catch ex As InvalidCastException
            MessageBox.Show("ETANextUpdate must be a valid date", "Row Errors on Incidents Table", MessageBoxButtons.OK, MessageBoxIcon.Error)
            [B]incidentsDataGridView.Item(e.ColumnIndex, e.RowIndex).Value = ""[/B]
        End Try
    Else
        incidentsDataGridView.CurrentCell = incidentsDataGridView.Item(e.ColumnIndex, e.RowIndex)
    End If

The bold line deletes the data enter in the current cell but the highlighted cell at the end of the event is the next cell. How do I keep the focus in the cell containing the error?

You may think that the Else clause is not executed, but it is. This code is in a CellValueChanged event which is triggered again when the entered data is cleared by the bold command. This then executes the Else clause.
 
The Else is not processed when the If part is. But why don't you use the CellValidating event to do your the cell validating? Set Cancel if the data doesn't validate and you're good. I think this should work:
VB.NET:
    Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) _
    Handles DataGridView1.CellValidating
        If Not Date.TryParse(e.FormattedValue, Nothing) Then e.Cancel = True
    End Sub
 
Many thanks for that John. It does the job almost perfectly. The code I am using is:

VB.NET:
                If Not Date.TryParse(CStr(e.FormattedValue), Nothing) Then
                    MessageBox.Show("Invalid date")
                    [B]DataGridView1.Item(e.ColumnIndex, e.RowIndex).Value = ""[/B]
                    e.Cancel = True
                End If

I am trying to blank out the entered string to make it easier to re-enter. The bold string worked when I was using the CellValueChanged event but doesn't blank it out in this event. Any suggestions?
 
The value has not been commited to the cell yet, when editing a datagridview cell it is the "editing control" you are working with.
VB.NET:
DataGridView1.EditingControl.ResetText()
 
Many thanks John

As a newcomer to VS.NET from VB2 - VB6 and Access 2 - Access 2003, I am finding the thought processes somewhat challenging. Even after 3 weeks of courses I am beginning to realise that even if you spent a year on courses, you still wouldn't know everything within VB.NET, ASP.NET and ADO.NET. It is very much a matter of continually looking at the methods and properties available at the time as you code and trying to pick tthe right one.
 
thanks

It would be advisable for you to find DataGridView class in documentation and browse through the members.

VB.NET:
DataGridView1.CurrentCell = DataGridView1.Item(1, 5)
'or
DataGridView1.CurrentCell = DataGridView1.Item("ColumnName", 5)

thanks johnH this is help me :)
 
Back
Top