Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / May 2008

Tip: Looking for answers? Try searching our database.

DataGridView causing problems

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tony K - 03 May 2008 23:21 GMT
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(ByVal msg As String)        'Called after serial port has
received data and trimmed the value
       CheckForIllegalCrossThreadCalls = 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.Inventory_management_databaseConnectionString)
       cnnInvMan.Open()

       strSQL = "SELECT * FROM Products WHERE ProductIDNumber = '" & msg &
"'"
       cmmInvMan = New OleDbCommand(strSQL, cnnInvMan)
       drdTest = cmmInvMan.ExecuteReader
       Do While drdTest.Read
           prodIDNum = CInt(drdTest.Item("ProductID"))
           description = drdTest.Item("ProductDescription").ToString
           loc = drdTest.Item("ProductLocation").ToString
           minimumOnHand = CInt(drdTest.Item("ReorderLevel"))
           maximumOnHand = CInt(drdTest.Item("MinimumRequired"))
       Loop

       drdTest.Close()

       strSQL = "SELECT SUM(UnitsReceived) - SUM(UnitsSold) -
SUM(UnitsShrinkage) AS UnitsOnHand " & _
               "FROM [" &
Me.Inventory_management_databaseDataSet.Inventory_Transactions.TableName &
"] WHERE (ProductID = " & prodIDNum & ")"

       cmmInvMan = New OleDbCommand(strSQL, cnnInvMan)
       drdTest = cmmInvMan.ExecuteReader
       Do While drdTest.Read
           Try
               units = CInt(drdTest.Item("UnitsOnHand"))
           Catch ex As InvalidCastException
               units = 0
           End Try
       Loop

       drdTest.Close()
       cnnInvMan.Close()

       Dim value As Integer
       Dim duplicateRow As DataGridViewRow

       For Each duplicateRow In Me.DataGridView1.Rows
           If duplicateRow.Cells(0).Value Is Nothing Then Exit For
           If duplicateRow.Cells(0).Value.ToString = msg Then
               'MessageBox.Show(duplicateRow.Cells(1).ToString)
               value = CInt(duplicateRow.Cells(4).Value)
               value += 1
               duplicateRow.Cells(4).Value = value.ToString
'unitsScanned
               If duplicateRow.Cells(1).Value Is Nothing Then  'description
column
                   duplicateRow.Cells(1).Value = description
               End If
               If duplicateRow.Cells(3).Value Is Nothing Then  'UnitsOnHand
                   duplicateRow.Cells(3).Value = units.ToString
               End If
               If duplicateRow.Cells(2).Value Is Nothing Then   'location
                   duplicateRow.Cells(2).Value = loc
               End If
               If duplicateRow.Cells(5).Value Is Nothing Then  'minOnHand
                   duplicateRow.Cells(5).Value = minimumOnHand.ToString
               End If
               If duplicateRow.Cells(6).Value Is Nothing Then   'maxOnHand
                   duplicateRow.Cells(6).Value = maximumOnHand.ToString
               End If
               Exit Sub
           End If
       Next

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

TIA,
Tony K.
Cor Ligthert[MVP] - 04 May 2008 07:22 GMT
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

> VB 2005 - Windows Vista
>
[quoted text clipped - 95 lines]
> TIA,
> Tony K.
Tony K - 04 May 2008 18:20 GMT
Thank you Cor.  I'll make the changes.

Tony K.

> Tony,
>
[quoted text clipped - 113 lines]
>> TIA,
>> Tony K.
Rich P - 05 May 2008 20:01 GMT
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.DataTable1.NewRow
dr("col1") = firstItemyouretrieved
dr("col2") = nextItemyouretrieved
dr("col3") = nextnextItem
..
dsData.DataTable1.Rows.Add(dr)
..
fill the table with your data
..

datagridview.DataSource = dsData.DataTable1

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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.