dcsimg
Results 1 to 5 of 5

Thread: [RESOLVED] DataGridView ScrolBar Bug

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2012
    Posts
    53

    Resolved [RESOLVED] DataGridView ScrolBar Bug

    When I get data for the DataGridView, my form freezes until the While loop completes, but then my scrollbar worked fine. I tried calling Application.DoEvents(); but that didn't work either.

    If I get the data in a thread, then my form does not freeze, but the scrollbar disables and does not work after the While completes. I tried a BackgroundWorker but the scrollbar has a problem when using that too.

    Code:
    Private Sub dg()
        myth = New Threading.Thread(AddressOf dgd)
        myth.IsBackground = True
        myth.Start()
    End Sub
    
    Private Sub dgd()
        Dim x As Integer
        If DataGridView1.Rows.Count = 0 Then x = 0 Else x = DataGridView1.Rows.Count
        Try
            Dim conn35a As New OleDbConnection("connstring")
            Dim cmd35a As New OleDbCommand
            cmd35a.CommandText = "Select count(*) from asd where downur Is Null"
            cmd35a.CommandType = CommandType.Text
            cmd35a.Connection = conn35a
            conn35a.Open()
            Dim returnValueaa As Integer = cmd35a.ExecuteScalar()
            conn35a.Close()
            Dim komut As String = "Select * from asd where downur Is Null"
            Dim conn2 As New OleDbConnection("connstring")
            conn2.Open()
            Dim cmd2 As New OleDbCommand(komut, conn2)
            Dim dr2 As OleDbDataReader = cmd2.ExecuteReader
            If dr2.HasRows Then
                While dr2.Read
                    Dim conn35 As New OleDbConnection("connstring")
                    Dim cmd35 As New OleDbCommand
                    cmd35.CommandText = "select count(*) from grid where ur = '" + dr2.Item("ur").ToString + "'"
                    cmd35.CommandType = CommandType.Text
                    cmd35.Connection = conn35
                    conn35.Open()
                    Dim returnValuea = cmd35.ExecuteScalar()
                    conn35.Close()
                    If returnValuea = 0 Then
                        DataGridView1.Rows.Add()
                        DataGridView1.Rows.Item(x).Cells(0).Value = x + 1
                        DataGridView1.Rows.Item(x).Cells(4).Value = "ID"
                        DataGridView1.Rows.Item(x).Cells(5).Value = dr2.Item("ur").ToString
                        DataGridView1.Rows.Item(x).Cells(6).Value = dr2.Item("ch").ToString
                        DataGridView1.Rows.Item(x).Cells(7).Value = dr2.Item("ti").ToString
                        DataGridView1.Rows.Item(x).Cells(8).Value = ".."
                        Dim client2 As New WebClient
                        Dim url As String = dr2.Item("pic").ToString
                        DataGridView1.Rows.Item(x).Cells(12).Value = New Bitmap(New MemoryStream(client2.DownloadData(url)))
                        DataGridView1.Rows.Item(x).Cells(13).Value = dr2.Item("vi")
                        DataGridView1.Rows.Item(x).Cells(14).Value = dr2.Item("su").ToString()
                        Dim con4 As New OleDbConnection("connstring") 
                        con4.Open()
                        Dim cmd5 = New OleDbCommand("INSERT INTO grid (ur) VALUES (@ur)", con4)
                        cmd5.CommandType = CommandType.Text
                        cmd5.Parameters.Add("@ur", OleDbType.VarChar, 500).Value = dr2.Item("ur").ToString
                        cmd5.ExecuteNonQuery()
                        con4.Close()
                        x += 1
                    End If
                End While
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,366

    Re: DataGridView ScrolBar Bug

    Of course the form freezes. You're executing code on the UI thread so it can't update the UI or respond to user input. Application.DoEvents is rarely a good solution to anything, as evidenced by your experience. What you should be doing is performing the work on a secondary thread, populating a list of some sort with the data and then binding that to the grid in one action on the UI thread.

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2012
    Posts
    53

    Re: DataGridView ScrolBar Bug

    Thank you for help.
    Give me simple basic example code please.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,366

    Re: DataGridView ScrolBar Bug

    Quote Originally Posted by forbid55 View Post
    Thank you for help.
    Give me simple basic example code please.
    You should first decide what type of list you're going to use. As you're working with a database, a DataTable seems a good first choice. Change your code to populate a DataTable and then bind that to the grid, i.e. assign the DataTable to the DataSource property of the grid.

    Once you've got the list populating correctly, then you can look at moving the data retrieval part to a secondary thread. Using a BackgroundWorker seems a good first option for that. You can follow the CodeBank link in my signature below to find a thread on Using The BackgroundWorker.

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2012
    Posts
    53

    Re: DataGridView ScrolBar Bug

    Okay.
    Thanks again.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width