Results 1 to 4 of 4

Thread: [RESOLVED] Datagridivew indexing problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    236

    Resolved [RESOLVED] Datagridivew indexing problem

    Using VB.net 2012 and a datagridview. I'm using a datatable to populate a datagridview programmatically. If I click a cell, the data on that row should be displayed in greater detail elsewhere. This is working fine.

    When the DGV is first loaded everything is fine ... I can scroll to further down, click on a row and the correct data is displayed. However, if I reload the DGV after that, things don't work as expected.

    This is where things get confusing .... (after reloading)
    If I click on a cell on the TOP part of the DGV (NOT move the scroll bar at all - any cell at the top page) things work just fine.
    HOWEVER ... if I scroll down even 1 line, the DGV shows the data as if I had clicked on the top page of the DGV (top page index). This is hard to explain ... there are 19 rows showing (out of 44 rows total in the table) on the top page of the DGV. If I click rows 1-19 everything is fine. But if I scroll down so that rows 2-20 are showing after a reload of the DGV and I click on row 20, the data from row 19 is displayed. If I then scroll anywhere I want before I reload the DGV and click on a cell the correct data is displayed. Here's another example .. if I scroll down and click on row 40 with the DGV completely scrolled to the end, row 15 of the DGV is displayed. But again, should I click anywhere else on the DGV before reloading it everything works fine.

    It seems to me that the index is not refreshing when I scroll to show a new page in the DGV after it is reloaded. Except that everything works fine when the DGV is first loaded. If the DGV is reloaded, the index is then wrong the first cell that is clicked but is fine for subsequent clicks .. until the DGV is reloaded when the index is messed up again.

    This is very hard to explain clearly ... here's a graphic to try to explain this ... The first time I load the DGV everything works perfectly. If I reload the DGV (after making changes for instance) the behavior becomes odd. At that point if I scroll down to the bottom row and click on that cell (Wynn in the graphic), the cell displayed is the one at the bottom row - BUT on the first page of the grid (Jackson on the graphic). This behavior is consistent for the first click after a reload. However, if I do not reload the grid but click on any other cell after this, the data displayed is correct.
    Name:  1800x1800.jpg
Views: 182
Size:  59.1 KB

    I've tried setting the selected row to 0 when reloading the DGV but this has no effect on the problem. This seems like something is not resetting when I reload the grid ... it only resets when I make the first click ... but I can't seem to figure it out. I know it's pretty weird ... but does anyone have any ideas?

    TIA
    Ken

  2. #2
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    404

    Re: Datagridivew indexing problem

    can you show use the code you are using to reload your data grid view and the code used on the cell content click event or which ever event you are using to handle the datagridview click

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    236

    Re: Datagridivew indexing problem

    These three subs are used to load the datatable (TABLE)

    Code:
    Imports System.Data.OleDb
    
    Public Class clsMDBDatabase
        Dim OLECmd As New OleDbCommand
        Dim OLEDA As New OleDbDataAdapter
    
    
        Public Function HasConnection(ByVal Fle As String) As Boolean
            If Fle = "B" Then Fle = BoardLocation
            If Fle = "G" Then Fle = GameLocation
            If Fle = "S" Then Fle = SeasonLocation
    
            Dim MDBConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Fle
            Dim cnn As OleDbConnection = New OleDbConnection(MDBConnString)
            Try
                cnn.Open()
                cnn.Close()
                Return True
            Catch ex As Exception
                'MsgBox(ex.Message)
                Return False
            End Try
        End Function
    
        Public Function FillDatatable(fle As String, Query As String) As Boolean
            If fle = "B" Then fle = BoardLocation
            If fle = "G" Then fle = GameLocation
            If fle = "S" Then fle = SeasonLocation
    
            Dim MDBConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & fle
            Dim cnn As OleDbConnection = New OleDbConnection(MDBConnString)
            Try
                cnn.Open()
                OLECmd = New OleDbCommand(Query, cnn)
                OLEDA = New OleDbDataAdapter(OLECmd)
                TABLE = New DataTable
    
                OLEDA.Fill(TABLE)
    
                cnn.Close()
                If TABLE.Rows.Count > 0 Then
                    Return True
                Else
                    Return False
                End If
    
            Catch ex As Exception
                If cnn.State = ConnectionState.Open Then cnn.Close()
                MsgBox(ex.Message)
                Return False
            End Try
        End Function
    Code:
     Private Sub LoadPlayers()
            Dim y As Integer = 1 'team counter
            If cboTeams.SelectedValue = 0 Then
                SQL = "SELECT Players.PlayerID, Teams.TeamName as [Team], Players.PlayerName as [Player], " &
                    "Players.PosRate, Players.Ratings, Players.Special " &
                    " FROM Teams INNER JOIN Players ON Teams.TeamID = Players.TeamID " &
                    "ORDER BY Teams.TeamName, Players.PlayerName"
            Else
                SQL = "SELECT Players.PlayerID, Teams.TeamName as [Team], Players.PlayerName as [Player], " &
                    "Players.PosRate, Players.Ratings, Players.Special " &
                    "FROM Teams INNER JOIN Players ON Teams.TeamID = Players.TeamID " &
                    "WHERE Players.TEAMID=" & cboTeams.SelectedValue & " " &
                    "ORDER BY Teams.TeamName, Players.PlayerName"
            End If
            dB.FillDatatable("S", SQL)
            LoadPlayerGrid()
    
            'find team count
            If TABLE.Rows.Count <> 0 Then
                Dim ps As String = TABLE.Rows(0).Item("Team")
                For x = 0 To TABLE.Rows.Count - 1
                    If TABLE.Rows(x).Item("Team") <> ps Then
                        ps = TABLE.Rows(x).Item("Team")
                        y = y + 1
                    End If
                Next
            End If
            lblTeamCount.Text = "Teams : " & y
            lblPlayerCount.Text = "Players : " & TABLE.Rows.Count
        End Sub
    Code:
     Public Sub LoadPlayerGrid()
            If TABLE.Rows.Count = 0 Then
                NoPlayers() ' subroutine that basically loads DGV with "No Teams on Record (empty TABLE)
                Exit Sub
            End If
    
            'add new column Positions
            TABLE.Columns.Add("Position")
            'parse posrate and put it in position location
            Dim x As Integer = 0
            'this sub finds the positions a player is qualified to play. IE (WideReceiver, OT, C, QB, etc)
            'it returns a string "pass" which is set into the position column
            For x = 0 To TABLE.Rows.Count - 1 'This sub finds and displays the position(s) the player is qualified to play(EB, OT etc)
                If ParsePositions(TABLE.Rows(x).Item("PosRate")) <> "empty" Then
                    TABLE.Rows(x).Item("Position") = Pass
                End If
            Next
    
            With frmMain.grdPlayers
    
                .DataSource = TABLE
                .AllowUserToAddRows = False
    
                .Columns("PlayerID").Visible = False
                .Columns("Ratings").Visible = False
                .Columns("Special").Visible = False
                .Columns("PosRate").Visible = False
    
                .RowHeadersVisible = False
                .Font = New Font("CourierNew", 10)
    
                'selection mode
                .SelectionMode = DataGridViewSelectionMode.FullRowSelect
                .MultiSelect = False
                .ReadOnly = True
                .AllowUserToResizeRows = False
    
                'header color
                .EnableHeadersVisualStyles = False
                .ColumnHeadersDefaultCellStyle.ForeColor = GridHeaderForecolor
                .ColumnHeadersDefaultCellStyle.BackColor = GridHeaderBackcolor
    
                'background color
                .BackgroundColor = GridBackgroundColor
                .GridColor = GridColor
    
                .Columns("Team").Width = 145
                .Columns("Player").Width = 160
                .Columns("Position").Width = 141
                '.Width = 220 + 80 + 160 + 3
    
                Dim DGVVerticalScroll = frmMain.grdTeams.Controls.OfType(Of VScrollBar).SingleOrDefault
                If DGVVerticalScroll.Visible Then .Width = .Width + SystemInformation.VerticalScrollBarWidth
    
                .Columns("Team").SortMode = DataGridViewColumnSortMode.Automatic
                .Columns("Player").SortMode = DataGridViewColumnSortMode.Automatic
                .Columns("Position").SortMode = DataGridViewColumnSortMode.Automatic
    
                'alignment()
                .Columns("Team").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
                .Columns("Player").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
                .Columns("Position").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
    
                .Columns("Team").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
                .Columns("Player").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
                .Columns("Position").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
    
                '.Rows(0).Selected = True
    
            End With
        End Sub
    Code:
    Private Sub grdPlayers_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles grdPlayers.CellClick
            If e.RowIndex = -1 Then 'header row clicked
                ClearPlayer() 'clears data from display area
                Exit Sub
            End If
            PlayerID = grdPlayers.SelectedRows(0).Cells("PlayerID").Value
            If PlayerID = 0 Then Exit Sub
            ClearPlayer()
            grpPlayer.Enabled = True
            cmdSavePlayer.Text = "Update Player"
            LoadPlayerData()
            txtPlayerName.Focus()
        End Sub

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    236

    Re: Datagridivew indexing problem

    Well, I found the problem although it makes absolutely no sense to me. It was disabling the group box that contained the detailed data. If I do not disable the box after updating the data, the indexing of the DGV remains stable regardless of the scroll bars. If I disable that group box then the indexing of the DGV which is in a different group box, gets all messed up. Interestingly, if I make the data group box visible/invisible, the indexing remains stable. It's only if I enable/disable the group box. I'm sure there is a reason for this, but it is definitely obscure to me.

    Thanks for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width