0
votes

I have a three column DataGridView and I want a user enter Tab in the last column to go to the next row but to column 1 instead of column 0. The following code gets an error System.StackOverflowException

I see code out there on how to change CurrentCell from buttons but the events (ChangeCurrentCell, CellLeave) seem to tolerate changing the current cell.

Private Sub dgvEngine_CellLeave(sender As Object, e As DataGridViewCellEventArgs) Handles dgvEngine.CellLeave
    If mbLoadEng = True Then Exit Sub
    If e.ColumnIndex = 2 Then
        dgvEngine.CurrentCell = dgvEngine(1, e.RowIndex + 1)
    End If
End Sub

How do I change the current cell from a DataGridView event?

I'm not limited to this approach - I'm just trying to skip Column 0 as the users tabs through the cells.


Found a better approach to my specific case - use the Row HeaderCell...

        dgvEngine.Rows(iRow).HeaderCell.Value = col.ColumnName.Substring(1)

later

        dgvEngine.AutoResizeRowHeadersWidth(
            DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders)

col.ColumnName is from a DataRow. What I am doing essentially is a transposed grid with DataRow column names as row headers.

1
How does it concern to c#? Why you tagged c#?Salah Akbari
I'm open to an answer written in C# - just broadens the audience. Plus C# are so much smarter than VB people... :)rheitzman
I self-answered sort of - I changed direction. The OP on changing the current cell could certainly be answered if someone is looking for points.rheitzman
@rheitzman This comment is primarily opinion based and is likely to raise a hot debate. toddelliott.net/2014/03/production/…haraman

1 Answers

0
votes

Figured out HeaderCell was the better approach. Result is an unbound grid with two sets of engine data (old vs new) side by side with row headers set to the fieldname.

    mbLoadEng = True
    Dim rowEng As DataRow
    dgvEngine.RowCount = 0
    If dgvEngines.SelectedRows.Count = 0 Then Exit Sub
    Dim sID As String = dgvEngines.SelectedRows(0).Cells(0).Value
    Dim a() As String
    Dim iRow As Integer
    Dim sWhere As String = " WHERE ID=" & sID

    Try

        Dim SQL As String = "SELECT  bYear, bMake, bModel, bSerial_Number, bEquipment_ID, bFuel_Type, bPower_Rating, bCyl, bLoad_Factor, bFuel_Consumption_Factor, "
        SQL &= "   bUnit_Conversion_Factor, bEmission_Category, bEmission_Family, bNOx_Emfac, bROG_Emfac, bPM_Emfac, bAnnual_Usage, bNOX_Emfac_Units, bROG_Emfac_Units, "
        SQL &= "   bPM_Emfac_Units, bAnnual_Usage_Units, bNotes"
        SQL &= " FROM     Engine_Equipment"

        rowEng = Gen.GetDataTable(SQL & sWhere).Rows(0)
        iRow = 0
        For Each col As DataColumn In rowEng.Table.Columns
            a = {rowEng(col).ToString, ""}
            dgvEngine.Rows.Add(a)
            dgvEngine.Rows(iRow).HeaderCell.Value = col.ColumnName.Substring(1)
            iRow += 1
        Next

        Sql = "SELECT  nYear, nMake, nModel, nSerial_Number, nEquipment_ID, nFuel_Type, nPower_Rating, nCyl, nLoad_Factor, "
        Sql &= "  nFuel_Consumption_Factor, nUnit_Conversion_Factor, nEmission_Category, nEmission_Family, nNOx_Emfac, nROG_Emfac, nPM_Emfac, nAnnual_Usage, "
        Sql &= "  nNOX_Emfac_Units, nROG_Emfac_Units, nPM_Emfac_Units, nAnnual_Usage_Units, nNotes"
        Sql &= " FROM     Engine_Equipment"

        rowEng = Gen.GetDataTable(SQL & sWhere).Rows(0)
        iRow = 0
        For Each col As DataColumn In rowEng.Table.Columns
            dgvEngine(1, iRow).Value = rowEng(col).ToString
            iRow += 1
        Next
        dgvEngine.AutoResizeColumns()
        dgvEngine.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders)
        dgvEngine.ClearSelection()
        dgvEngine.Columns(0).ReadOnly = True
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        mbLoadEng = False
    End Try