简体   繁体   English

仅遍历包含dataTable中特定行的值的列

[英]Looping through only columns containing values for specific row in dataTable

I am developing an application using Visual Basic 2010 for hydraulic calculations of a pipe network. 我正在开发使用Visual Basic 2010进行管网水力计算的应用程序。

This application uses a lot of iterations and loops, depending on the user input and size of network. 此应用程序使用大量的迭代和循环,具体取决于用户的输入和网络的大小。 Most of the results have to be saved temporarily to be used for the next step of calculations. 大多数结果必须暂时保存才能用于下一步计算。

Firstly, I used a DataGridView to save the results but as the number of iterations increased, the application became very slow. 首先,我使用DataGridView保存结果,但是随着迭代次数的增加,应用程序变得非常慢。

Now I am trying to create a DataTable , then populate it with some initial results (this part was successful). 现在,我尝试创建一个DataTable ,然后用一些初始结果填充它(这部分成功了)。 The obtained DataTable has some columns that are not populated like so: 获得的DataTable有一些未填充的列,如下所示:

22 24 10                                              
3  16 22 9 15                                             
16 12 24 13                                             
14 21 10 23 12 1                                            
24 18 23 2  1                                           

Other calculations are performed and a certain value (X) is obtained. 执行其他计算,并获得确定值(X)

Now I am trying to loop through the columns of a specific row to check if the calculated value (X) equals to one of the values in those columns. 现在,我尝试遍历特定行的列,以检查计算出的值(X)等于这些列中的值之一。

My question is : How can I loop through only the columns that have values (avoiding the columns containing NULL values) for a specific row? 我的问题是 :如何仅遍历具有特定行的值的列(避免包含NULL值的列)?

I am a beginner in VB.net. 我是VB.net的初学者。 I hope my question is clear as I didn't provide any code. 我希望我的问题很清楚,因为我没有提供任何代码。

Thanks in advance for you help. 在此先感谢您的帮助。

This is the initial code I used: 这是我使用的初始代码:

           Results.DGVInitial.Rows.Clear()
           Results.DGVFinal.Rows.Clear()

                For m As Integer = 0 To NetworkLayout.DGVNetworkLayout.Rows.Count - 1
                    Results.DGVInitial.Rows.Add()
                Next


                Dim I As Integer = NetworkLayout.DGVNetworkLayout.Rows.Count - 1

                Dim Sec(I), Ini(I) As Integer            
                Dim Hyd(I), Dia(I), Len(I) As Single      
                Dim Qsec(I), Qini(I), Vsec(I) As Single   
                Dim U(I), Y(I) As Single                 

                Do

                    I = I - 1

                    Sec(I) = NetworkLayout.DGVNetworkLayout.Rows(I).Cells(0).Value
                    Ini(I) = NetworkLayout.DGVNetworkLayout.Rows(I).Cells(1).Value
                    Hyd(I) = NetworkLayout.DGVNetworkLayout.Rows(I).Cells(6).Value
                    Dia(I) = NetworkLayout.DGVNetworkLayout.Rows(I).Cells(4).Value
                    Len(I) = NetworkLayout.DGVNetworkLayout.Rows(I).Cells(3).Value

                    Dim V As Integer
                    V = Results.DGVRandomGen.Rows(TotalNum_Runs - 1).Cells(I).Value    
                    Qsec(I) = 0
                    Dim q As Single = 0

                    For n As Integer = 0 To Results.DGVInitial.Rows.Count - 1    
                        If Results.DGVInitial.Rows(n).Cells(1).Value = Sec(I) Then
                            q = Results.DGVInitial.Rows(n).Cells(0).Value
                            Qsec(I) = Qsec(I) + q
                        Else
                            Qsec(I) = Qsec(I)

                        End If
                    Next

                    If V = 1 Then ' if the hydrant is open
                       Qini(I) = Hyd(I) + Qsec(I)
                    Else ' if the hydrant is close
                       Qini(I) = Qsec(I)
                    End If


                    Results.DGVInitial.Rows(I).Cells(0).Value = Qini(I)
                    Results.DGVInitial.Rows(I).Cells(1).Value = Ini(I)

Results.DGVSectionDischarges.Rows(TotalNum_Runs - 1).Cells(I).Value = ini(I).ToString("F2") Results.DGVSectionDischarges.Rows(TotalNum_Runs-1).Cells(I).Value = ini(I).ToString(“ F2”)

Now instead of using 现在,而不是使用

V = Results.DGVRandomGen.Rows(TotalNum_Runs - 1).Cells(I).Value V = Results.DGVRandomGen.Rows(TotalNum_Runs-1).Cells(I).Value

I would like to replace the "DGVRandomGen" with a DataTable called "DT_Random" 我想用名为“ DT_Random”的数据表替换“ DGVRandomGen”

Like I said I am a beginner so I am not sure how to code it but it will be something like this: 就像我说的我是一个初学者,所以我不确定如何编写代码,但是它将是这样的:

For DT_Random.Rows (TotalNum_Runs - 1) 对于DT_Random.Rows(TotalNum_Runs-1)

               For Each col As DataColumn In DT_Random.Columns
                    If DT_Random.Rows(TotalNum_Runs - 1).Item(col) = I Then
                        Qini(I) = Hyd(I) + Qsec(I)
                    Else 
                        Qini(I) = Qsec(I)
                    End If
                Next

But I want to avoid Null values as not all columns are populated 但我想避免使用Null值,因为并非所有列都已填充

Thanks 谢谢

Maybe this will help you: 也许这会帮助您:

    Dim myXvalue = 24

    Dim myDataTable As New DataTable

    myDataTable.Columns.Add("Col1")
    myDataTable.Columns.Add("Col2")
    myDataTable.Columns.Add("Col3")
    myDataTable.Columns.Add("Col4")


    myDataTable.Rows.Add(22, 24, 10, DBNull.Value)
    myDataTable.Rows.Add(3, 16, 22, DBNull.Value)
    myDataTable.Rows.Add(24, 18, DBNull.Value, 24)

    For Each column As DataColumn In myDataTable.Columns

        If IsDBNull(myDataTable.Rows(0).Item(column)) Then
            MsgBox("DB Null Found At: " & column.ColumnName)
            Continue For
        End If

        If myDataTable.Rows(0).Item(column) = myXvalue Then
            MsgBox("Match: " & myDataTable.Rows(0).Item(column) & " found at " & column.ColumnName)
        End If

    Next column

Just a quick example, you may need to restructure it a bit, but at least it shows you how to access the values in your datatable by columns. 只是一个简单的示例,您可能需要对其进行一些重组,但至少它显示了如何按列访问数据表中的值。 I would do a function that passes a row index as a parameter and returns a boolean. 我会做一个传递行索引作为参数并返回布尔值的函数。 Create two booleans inside the sub, one for dbnull existing in the row, and one for finding a matching value. 在子内部创建两个布尔,一个用于行中存在的dbnull,另一个用于查找匹配值。 If dbnull bool is false, and match value is true, then return true. 如果dbnull bool为false,并且match值为true,则返回true。 Just make sure you loop all the columns and dont exit early. 只要确保您循环所有列并且不要提早退出即可。

If you need me to elaborate let me know. 如果您需要我详细说明,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM