Results 1 to 4 of 4

Thread: Datagridview behaviour problem

Threaded View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    67

    Datagridview behaviour problem

    Some time ago I wrote a program in vb that imports data from an Access database into a datagridview. The imported file leaves some cells unpopulated. The program performs a series of calculations based on the contents of various cells in the grid and writes the results to the empty cells. Ultimately, the program writes the grid back to the original Access database. All of this works great with no problems; however, I decided to get the datagridview information directly from an Excel spreadsheet using:

    Code:
    Imports System.IO
    Imports ExcelDataReader
    
    Public Class Form1
        Dim tables As DataTableCollection
        Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
            Using ofd As OpenFileDialog = New OpenFileDialog() With {.Filter = "Excel 97-2003 Workbook | *.xlsx"}
                If ofd.ShowDialog() = DialogResult.OK Then
                    txtFileName.Text = ofd.FileName
                    Using stream = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read)
                        Using reader As IExcelDataReader = ExcelReaderFactory.CreateReader(stream)
                            Dim result As DataSet = reader.AsDataSet(New ExcelDataSetConfiguration() With {
                                                                       .ConfigureDataTable = Function(__) New ExcelDataTableConfiguration() With {
                                                                       .UseHeaderRow = True}})
                            tables = result.Tables
                            cboSheet.Items.Clear()
                            For Each table As DataTable In tables
                                cboSheet.Items.Add(table.TableName)
                            Next
                        End Using
                    End Using
                End If
            End Using
        End Sub
    Code:
    Private Sub cboSheet_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboSheet.SelectedIndexChanged
            Dim dt As DataTable = tables(cboSheet.SelectedItem.ToString())
            dgvPrice.DataSource = dt
        End Sub
    This imports the data fine, but, if I use the same code for populating empty cells as I did with the Access import, the empty cells don't populate even though, in debug mode, I can see that the variables are calculating correctly. It simply won't populate the cells in the grid and I can't work out why it works in the first instance and not in the second.
    The following code is being used in both cases to populate the empty cells in both cases - works in one and not in the other.

    Code:
    Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
            Dim cpc As Integer
            Dim tot(300)
            Dim c As Integer
            Dim smallmin As Double
    
            For i = 0 To dgvPrice.RowCount - 1
                If IsDBNull(dgvPrice.Rows(i).Cells(21).Value) Then 'References CountPriceComp - if it's null...............................
                    If Not IsDBNull(dgvPrice.Rows(i).Cells(23).Value) Then 'References BestDTB - if it's not null..................................
                        dgvPrice.Rows(i).Cells(26).Value = dgvPrice.Rows(i).Cells(25).Value 'MinFromSelection = Selection
                    End If
                End If
                If Not IsDBNull(dgvPrice.Rows(i).Cells(21).Value) Then 'References CountPriceComp - if it's not null...............................
    
                    cpc = dgvPrice.Rows(i).Cells(21).Value 'set variable cpc to be value of count price compare code
                    c = i 'sets variable c to row index
    
                    For x = 0 To cpc - 1 'sets variable x to count of price compare code and cycles through rows to get selection prices for the number of same price compare codes
                        tot(x) = dgvPrice.Rows(c).Cells(25).Value
                        If c < dgvPrice.RowCount - 1 Then
                            c = c + 1
                        ElseIf c > dgvPrice.RowCount - 1 Then
                            Exit Sub
                        End If
                    Next x
                    For x = 0 To cpc - 1
                        If Not IsDBNull(tot(x)) Then
                            If tot(x) > 0 Then smallmin = tot(x)
                        End If
                    Next x
                    'ASSIGN SMALL MIN TO MIMIMUM FROM SELECTION COLUMN
                    For y = 0 To cpc - 1
                        If Not IsDBNull(tot(y)) Then
                            If tot(y) < smallmin And tot(y) <> 0 Then smallmin = tot(y)
                        End If
                    Next y
                    For j = 0 To cpc - 1
                        dgvPrice.Rows(i).Cells(26).Value = smallmin
                        i = i + 1
                    Next j
                    i = i - 1
                    cpc = 0
                    smallmin = 0
                End If
    
            Next i
        End Sub
    Any help would be appreciated.
    Additionally, the code used for importing the Access database is:
    Code:
     Private Sub Pricing_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Using OleDBConn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Access\Test.mdb;")
    
                Using Phase7DA As New OleDb.OleDbDataAdapter("select * from UploadPricing order by PriceComp", OleDBConn)
                    With Phase7DA
                        .MissingSchemaAction = MissingSchemaAction.AddWithKey
                        .Fill(Dset, "Pricing")
                    End With
                End Using
            End Using
            DgvPrice.DataSource = Dset.Tables(0)
    End sub
    Last edited by Pianoman23; May 14th, 2020 at 03:29 AM.

Tags for this Thread

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