Dule Marin
Dule Marin

Reputation: 3

DataTable.Rows.Count always returns 0 - VB.NET

I'm trying to populate DataGridView cells with information from two (parent, child tables). Everything works just fine, except for this part table.Rows.Count. It always returns 0, no matter that I have one record in the table.

This is the code:

    Private Sub Query__tbl_purchase_order_articlesDataGridView_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles Query__tbl_purchase_order_articlesDataGridView.CellEndEdit

    Dim table As DataTable = Orders_DBDataSet.Tables("tbl_list_of_articles")


    Dim selectedRowCount As Integer = Query__tbl_purchase_order_articlesDataGridView.CurrentCell.RowIndex 
    Dim art_ID As Integer = CInt(Query__tbl_purchase_order_articlesDataGridView.Rows(selectedRowCount).Cells(0).Value)

    Dim art_ttl As String
    Dim art_ttl_tr As String
    Dim mu As String

    Dim art_ttl_i As Integer
    Dim art_ttl_tr_i As Integer
    Dim mu_i As Integer

    Dim art_ID_t_i As Integer
    Dim art_ttl_t_i As Integer
    Dim art_ttl_tr_t_i As Integer
    Dim mu_t_i As Integer

    Dim i As Integer

    'search for column index in DataGridView
    For i = 0 To Query__tbl_purchase_order_articlesDataGridView.Columns.Count - 1
        If Query__tbl_purchase_order_articlesDataGridView.Columns(i).HeaderText.ToString = "article_name" Then
            art_ttl_i = i
        End If
        If Query__tbl_purchase_order_articlesDataGridView.Columns(i).HeaderText.ToString = "article_name_tr" Then
            art_ttl_tr_i = i
        End If
        If Query__tbl_purchase_order_articlesDataGridView.Columns(i).HeaderText.ToString = "masurement_unit" Then
            mu_i = i
        End If
    Next

    'search for column index in "table"
    For i = 0 To table.Columns.Count - 1
        If table.Columns(i).ColumnName.ToString = "article_ID" Then
            art_ID_t_i = i
        End If
        If table.Columns(i).ColumnName.ToString = "article_name" Then
            art_ttl_t_i = i
        End If
        If table.Columns(i).ColumnName.ToString = "article_name_tr" Then
            art_ttl_tr_t_i = i
        End If
        If table.Columns(i).ColumnName.ToString = "masurement_unit" Then
            mu_t_i = i
        End If
    Next

    For i = 0 To table.Rows.Count
        If art_ID = CInt(table.Rows(i).Item(art_ID_t_i).ToString()) Then
            art_ttl = table.Rows(i).Item(art_ttl_t_i).ToString()
            art_ttl_tr = table.Rows(i).Item(art_ttl_tr_t_i).ToString()
            mu = table.Rows(i).Item(mu_t_i).ToString()

            Query__tbl_purchase_order_articlesDataGridView.Rows(selectedRowCount).Cells(art_ttl_i).Value = art_ttl
            Query__tbl_purchase_order_articlesDataGridView.Rows(selectedRowCount).Cells(art_ttl_tr_i).Value = art_ttl_tr
            Query__tbl_purchase_order_articlesDataGridView.Rows(selectedRowCount).Cells(mu_i).Value = mu

            Exit For
        End If

    Next

End Sub

Also, I tied to test it like this, but the result is the same. It can find a table index, but always return 0 as a table row count.

'test ========================================
    Dim x, y As Integer
    For x = 0 To Orders_DBDataSet.Tables.Count - 1
        If Orders_DBDataSet.Tables(x).TableName.ToString = "tbl_list_of_articles" Then
            y = x
        End If
    Next

    x = 0

    For Each row As DataRow In table.Rows
        x = x + 1
    Next
    '============================================
    x = Orders_DBDataSet.Tables(y).Rows.Count

Upvotes: 0

Views: 1682

Answers (1)

Gabriel Stancu
Gabriel Stancu

Reputation: 4260

Most of the times this happened to me because I was not actually using the instance of the dataset "connected" to the form. What does this mean? You either don't populate the dataset instance tied to the form in the Load event, or you are populating it but you are somehow using other instance of it. This has a higher chance of happening if you are sending data between forms as well.

Upvotes: 2

Related Questions