DataGridView causing problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony K

    #1

    DataGridView causing problems

    VB 2005 - Windows Vista

    I have a form that seems to lock up when the number of rows exceed the
    height of the datagridview. The following code executes when data is
    received through the serial port (ie. barcode scanning). It seems that
    everything works GREAT and processes as it was meant to be until the rows
    exceed the display area. I have 7 columns. The Description column is the
    only column with the AutoSizeMode set to Fill. The datagridview is docked
    to the top. DataSource and DataMember for the DGV is not set to any
    dataset.

    I have the vertical scrollbar property set but it does not show up most of
    the time. When/If it does, when I try and utilize the scrollbar, it locks
    up the application immediately. I don't understand but I think it has to do
    with the following method.


    Sub ReceiveData(ByV al msg As String) 'Called after serial port has
    received data and trimmed the value
    CheckForIllegal CrossThreadCall s = False
    description = Nothing 'form instance variable
    loc = Nothing
    Dim cmmInvMan As OleDbCommand
    Dim strSQL As String
    Dim drdTest As OleDbDataReader
    Dim cnnInvMan As OleDbConnection = New
    OleDbConnection (My.Settings.In ventory_managem ent_databaseCon nectionString)
    cnnInvMan.Open( )

    strSQL = "SELECT * FROM Products WHERE ProductIDNumber = '" & msg &
    "'"
    cmmInvMan = New OleDbCommand(st rSQL, cnnInvMan)
    drdTest = cmmInvMan.Execu teReader
    Do While drdTest.Read
    prodIDNum = CInt(drdTest.It em("ProductID") )
    description = drdTest.Item("P roductDescripti on").ToString
    loc = drdTest.Item("P roductLocation" ).ToString
    minimumOnHand = CInt(drdTest.It em("ReorderLeve l"))
    maximumOnHand = CInt(drdTest.It em("MinimumRequ ired"))
    Loop

    drdTest.Close()

    strSQL = "SELECT SUM(UnitsReceiv ed) - SUM(UnitsSold) -
    SUM(UnitsShrink age) AS UnitsOnHand " & _
    "FROM [" &
    Me.Inventory_ma nagement_databa seDataSet.Inven tory_Transactio ns.TableName &
    "] WHERE (ProductID = " & prodIDNum & ")"

    cmmInvMan = New OleDbCommand(st rSQL, cnnInvMan)
    drdTest = cmmInvMan.Execu teReader
    Do While drdTest.Read
    Try
    units = CInt(drdTest.It em("UnitsOnHand "))
    Catch ex As InvalidCastExce ption
    units = 0
    End Try
    Loop

    drdTest.Close()
    cnnInvMan.Close ()

    Dim value As Integer
    Dim duplicateRow As DataGridViewRow

    For Each duplicateRow In Me.DataGridView 1.Rows
    If duplicateRow.Ce lls(0).Value Is Nothing Then Exit For
    If duplicateRow.Ce lls(0).Value.To String = msg Then
    'MessageBox.Sho w(duplicateRow. Cells(1).ToStri ng)
    value = CInt(duplicateR ow.Cells(4).Val ue)
    value += 1
    duplicateRow.Ce lls(4).Value = value.ToString
    'unitsScanned
    If duplicateRow.Ce lls(1).Value Is Nothing Then 'description
    column
    duplicateRow.Ce lls(1).Value = description
    End If
    If duplicateRow.Ce lls(3).Value Is Nothing Then 'UnitsOnHand
    duplicateRow.Ce lls(3).Value = units.ToString
    End If
    If duplicateRow.Ce lls(2).Value Is Nothing Then 'location
    duplicateRow.Ce lls(2).Value = loc
    End If
    If duplicateRow.Ce lls(5).Value Is Nothing Then 'minOnHand
    duplicateRow.Ce lls(5).Value = minimumOnHand.T oString
    End If
    If duplicateRow.Ce lls(6).Value Is Nothing Then 'maxOnHand
    duplicateRow.Ce lls(6).Value = maximumOnHand.T oString
    End If
    Exit Sub
    End If
    Next

    Dim row As String() = {msg, description, loc, units.ToString, "1",
    minimumOnHand.T oString, maximumOnHand.T oString}
    Me.DataGridView 1.Rows.Add(row)
    End Sub

    TIA,
    Tony K.

  • Cor Ligthert[MVP]

    #2
    Re: DataGridView causing problems

    Tony,

    Try to avoid working direct with the DataGridView. That is only a UI, not a
    datacollection.

    Use datasources where for VB 2005 and the DataGridView the datatable is in
    my idea the best and use for retrieving Data the dataadapter (or
    tableadabter) as those give you the best performance.

    Work then direct in the underlying DataSource (DataTable)

    Be aware that setting something to nothing is a little bit strange.
    Try to declare as less global as possible, because a unnecessary global
    variable gives only confusion and uses more memory.

    Cor

    "Tony K" <myfirstname@ki ngprogramming.c omschreef in bericht
    news:FD1867D0-E787-46A2-9C1D-FB35D425A15A@mi crosoft.com...
    VB 2005 - Windows Vista
    >
    I have a form that seems to lock up when the number of rows exceed the
    height of the datagridview. The following code executes when data is
    received through the serial port (ie. barcode scanning). It seems that
    everything works GREAT and processes as it was meant to be until the rows
    exceed the display area. I have 7 columns. The Description column is the
    only column with the AutoSizeMode set to Fill. The datagridview is docked
    to the top. DataSource and DataMember for the DGV is not set to any
    dataset.
    >
    I have the vertical scrollbar property set but it does not show up most of
    the time. When/If it does, when I try and utilize the scrollbar, it locks
    up the application immediately. I don't understand but I think it has to
    do with the following method.
    >
    >
    Sub ReceiveData(ByV al msg As String) 'Called after serial port has
    received data and trimmed the value
    CheckForIllegal CrossThreadCall s = False
    description = Nothing 'form instance variable
    loc = Nothing
    Dim cmmInvMan As OleDbCommand
    Dim strSQL As String
    Dim drdTest As OleDbDataReader
    Dim cnnInvMan As OleDbConnection = New
    OleDbConnection (My.Settings.In ventory_managem ent_databaseCon nectionString)
    cnnInvMan.Open( )
    >
    strSQL = "SELECT * FROM Products WHERE ProductIDNumber = '" & msg &
    "'"
    cmmInvMan = New OleDbCommand(st rSQL, cnnInvMan)
    drdTest = cmmInvMan.Execu teReader
    Do While drdTest.Read
    prodIDNum = CInt(drdTest.It em("ProductID") )
    description = drdTest.Item("P roductDescripti on").ToString
    loc = drdTest.Item("P roductLocation" ).ToString
    minimumOnHand = CInt(drdTest.It em("ReorderLeve l"))
    maximumOnHand = CInt(drdTest.It em("MinimumRequ ired"))
    Loop
    >
    drdTest.Close()
    >
    strSQL = "SELECT SUM(UnitsReceiv ed) - SUM(UnitsSold) -
    SUM(UnitsShrink age) AS UnitsOnHand " & _
    "FROM [" &
    Me.Inventory_ma nagement_databa seDataSet.Inven tory_Transactio ns.TableName &
    "] WHERE (ProductID = " & prodIDNum & ")"
    >
    cmmInvMan = New OleDbCommand(st rSQL, cnnInvMan)
    drdTest = cmmInvMan.Execu teReader
    Do While drdTest.Read
    Try
    units = CInt(drdTest.It em("UnitsOnHand "))
    Catch ex As InvalidCastExce ption
    units = 0
    End Try
    Loop
    >
    drdTest.Close()
    cnnInvMan.Close ()
    >
    Dim value As Integer
    Dim duplicateRow As DataGridViewRow
    >
    For Each duplicateRow In Me.DataGridView 1.Rows
    If duplicateRow.Ce lls(0).Value Is Nothing Then Exit For
    If duplicateRow.Ce lls(0).Value.To String = msg Then
    'MessageBox.Sho w(duplicateRow. Cells(1).ToStri ng)
    value = CInt(duplicateR ow.Cells(4).Val ue)
    value += 1
    duplicateRow.Ce lls(4).Value = value.ToString 'unitsScanned
    If duplicateRow.Ce lls(1).Value Is Nothing Then
    'description column
    duplicateRow.Ce lls(1).Value = description
    End If
    If duplicateRow.Ce lls(3).Value Is Nothing Then
    'UnitsOnHand
    duplicateRow.Ce lls(3).Value = units.ToString
    End If
    If duplicateRow.Ce lls(2).Value Is Nothing Then 'location
    duplicateRow.Ce lls(2).Value = loc
    End If
    If duplicateRow.Ce lls(5).Value Is Nothing Then 'minOnHand
    duplicateRow.Ce lls(5).Value = minimumOnHand.T oString
    End If
    If duplicateRow.Ce lls(6).Value Is Nothing Then 'maxOnHand
    duplicateRow.Ce lls(6).Value = maximumOnHand.T oString
    End If
    Exit Sub
    End If
    Next
    >
    Dim row As String() = {msg, description, loc, units.ToString, "1",
    minimumOnHand.T oString, maximumOnHand.T oString}
    Me.DataGridView 1.Rows.Add(row)
    End Sub
    >
    TIA,
    Tony K.

    Comment

    • Tony K

      #3
      Re: DataGridView causing problems

      Thank you Cor. I'll make the changes.

      Tony K.

      "Cor Ligthert[MVP]" <notmyfirstname @planet.nlwrote in message
      news:3DBA23F1-9319-4FE2-9490-204D2A5231E8@mi crosoft.com...
      Tony,
      >
      Try to avoid working direct with the DataGridView. That is only a UI, not
      a datacollection.
      >
      Use datasources where for VB 2005 and the DataGridView the datatable is in
      my idea the best and use for retrieving Data the dataadapter (or
      tableadabter) as those give you the best performance.
      >
      Work then direct in the underlying DataSource (DataTable)
      >
      Be aware that setting something to nothing is a little bit strange.
      Try to declare as less global as possible, because a unnecessary global
      variable gives only confusion and uses more memory.
      >
      Cor
      >
      "Tony K" <myfirstname@ki ngprogramming.c omschreef in bericht
      news:FD1867D0-E787-46A2-9C1D-FB35D425A15A@mi crosoft.com...
      >VB 2005 - Windows Vista
      >>
      >I have a form that seems to lock up when the number of rows exceed the
      >height of the datagridview. The following code executes when data is
      >received through the serial port (ie. barcode scanning). It seems that
      >everything works GREAT and processes as it was meant to be until the rows
      >exceed the display area. I have 7 columns. The Description column is
      >the only column with the AutoSizeMode set to Fill. The datagridview is
      >docked to the top. DataSource and DataMember for the DGV is not set to
      >any dataset.
      >>
      >I have the vertical scrollbar property set but it does not show up most
      >of the time. When/If it does, when I try and utilize the scrollbar, it
      >locks up the application immediately. I don't understand but I think it
      >has to do with the following method.
      >>
      >>
      >Sub ReceiveData(ByV al msg As String) 'Called after serial port has
      >received data and trimmed the value
      > CheckForIllegal CrossThreadCall s = False
      > description = Nothing 'form instance variable
      > loc = Nothing
      > Dim cmmInvMan As OleDbCommand
      > Dim strSQL As String
      > Dim drdTest As OleDbDataReader
      > Dim cnnInvMan As OleDbConnection = New
      >OleDbConnectio n(My.Settings.I nventory_manage ment_databaseCo nnectionString)
      > cnnInvMan.Open( )
      >>
      > strSQL = "SELECT * FROM Products WHERE ProductIDNumber = '" & msg
      >& "'"
      > cmmInvMan = New OleDbCommand(st rSQL, cnnInvMan)
      > drdTest = cmmInvMan.Execu teReader
      > Do While drdTest.Read
      > prodIDNum = CInt(drdTest.It em("ProductID") )
      > description = drdTest.Item("P roductDescripti on").ToString
      > loc = drdTest.Item("P roductLocation" ).ToString
      > minimumOnHand = CInt(drdTest.It em("ReorderLeve l"))
      > maximumOnHand = CInt(drdTest.It em("MinimumRequ ired"))
      > Loop
      >>
      > drdTest.Close()
      >>
      > strSQL = "SELECT SUM(UnitsReceiv ed) - SUM(UnitsSold) -
      >SUM(UnitsShrin kage) AS UnitsOnHand " & _
      > "FROM [" &
      >Me.Inventory_m anagement_datab aseDataSet.Inve ntory_Transacti ons.TableName
      >& "] WHERE (ProductID = " & prodIDNum & ")"
      >>
      > cmmInvMan = New OleDbCommand(st rSQL, cnnInvMan)
      > drdTest = cmmInvMan.Execu teReader
      > Do While drdTest.Read
      > Try
      > units = CInt(drdTest.It em("UnitsOnHand "))
      > Catch ex As InvalidCastExce ption
      > units = 0
      > End Try
      > Loop
      >>
      > drdTest.Close()
      > cnnInvMan.Close ()
      >>
      > Dim value As Integer
      > Dim duplicateRow As DataGridViewRow
      >>
      > For Each duplicateRow In Me.DataGridView 1.Rows
      > If duplicateRow.Ce lls(0).Value Is Nothing Then Exit For
      > If duplicateRow.Ce lls(0).Value.To String = msg Then
      > 'MessageBox.Sho w(duplicateRow. Cells(1).ToStri ng)
      > value = CInt(duplicateR ow.Cells(4).Val ue)
      > value += 1
      > duplicateRow.Ce lls(4).Value = value.ToString 'unitsScanned
      > If duplicateRow.Ce lls(1).Value Is Nothing Then
      >'description column
      > duplicateRow.Ce lls(1).Value = description
      > End If
      > If duplicateRow.Ce lls(3).Value Is Nothing Then
      >'UnitsOnHand
      > duplicateRow.Ce lls(3).Value = units.ToString
      > End If
      > If duplicateRow.Ce lls(2).Value Is Nothing Then 'location
      > duplicateRow.Ce lls(2).Value = loc
      > End If
      > If duplicateRow.Ce lls(5).Value Is Nothing Then 'minOnHand
      > duplicateRow.Ce lls(5).Value = minimumOnHand.T oString
      > End If
      > If duplicateRow.Ce lls(6).Value Is Nothing Then
      >'maxOnHand
      > duplicateRow.Ce lls(6).Value = maximumOnHand.T oString
      > End If
      > Exit Sub
      > End If
      > Next
      >>
      > Dim row As String() = {msg, description, loc, units.ToString, "1",
      >minimumOnHand. ToString, maximumOnHand.T oString}
      > Me.DataGridView 1.Rows.Add(row)
      >End Sub
      >>
      >TIA,
      >Tony K.
      >

      Comment

      • Rich P

        #4
        Re: DataGridView causing problems

        Hello,

        Instead of trying to display the date you retrieve from the serial port
        directly in the datagridview, do this: Create an empty dataset in the
        DataSources tab of Visual Studio - next to the Explorer/Solution tab
        (usually on the right side of the screen or wherever you have set the
        Solution explorer). In the datasources tab just use the wizard to
        create an empty dataset and don't select any tables. Once you have an
        empty dataset you can then edit the dateset (right click on the new
        dataset and select edit from the context menu). Once you are inside the
        dataset window - right click and select Add Table. Create a table that
        will contains the fields you need - you an assign datatypes, but the
        default is string. Name the table something - the default name is
        "DataTable1 "

        Now, in your form code, you can declare a dataset var as the dataset you
        just created

        Dim dsData As MyNewDataset 'or whatever you name the dataset in the
        datasources tab

        dsData = New MyNewDataset
        Dim dr As DataRow = dsData.DataTabl e1.NewRow
        dr("col1") = firstItemyouret rieved
        dr("col2") = nextItemyouretr ieved
        dr("col3") = nextnextItem
        ...
        dsData.DataTabl e1.Rows.Add(dr)
        ...
        fill the table with your data
        ...

        datagridview.Da taSource = dsData.DataTabl e1

        You can then use the dataAdapter to transfer the data to a server DB.
        Note: the dataAdapter doesn't work with an OLEDB db (MS Access) - only
        works with Server DBs. For OLEDB you can use an ADO.Net command object
        - works almost the same as classic ADO command object.


        Rich

        *** Sent via Developersdex http://www.developersdex.com ***

        Comment

        Working...