加入 2 tables,将第一个 table 添加为 datagridview 的列 headers,然后将第二个 table 添加为列 headers 下方的行

Join 2 tables, add first table as column headers for datagridview then add second table as rows below column headers

我想为 FACILITIES 添加列,然后使用他们的 fac_id 从另一个 table 中找到他们的产水量以显示为单个headers.

列下方的行
Do While tableRetrieve.Read = True

table.Columns.Add(tableRetrieve("facility"), Type.GetType("System.String"))

Dim newDate As DateTime = DateTime.ParseExact(dtpFrom.Value, "MM/dd/yyyy h:m:s tt",
  System.Globalization.DateTimeFormatInfo.InvariantInfo)
Dim myDate As String = newDate.ToString("yyyy-MM-dd", System.Globalization.DateTimeFormatInfo.InvariantInfo)

If myDate = tableRetrieve("sentDate") Then

If tableRetrieve("prod_id") = tableRetrieve("fac_id") Then
table.Rows.Add(tableRetrieve("facility_produce"))
End If

End If

Loop

我可以添加列 headers 但在行中失败。

下面是输出图像。显示第headers列,但产水量只显示一行。根据prod_id等于fac_id.

每个对应的列应该有产水量

Sample of DataGridView

更新:

这就是我的代码现在的样子。如果只有一行,这很有效。如果还有另一行,则会显示错误。

Do While tableRetrieve.Read = True

  table.Columns.Add(tableRetrieve("facility"), Type.GetType("System.String"))

  If tableRetrieve("facility_id") = tableRetrieve("fac_id") Then

    newDate = tableRetrieve("sentDate")

    If prevDate = newDate Then

    table.Rows(row)(col) = tableRetrieve("facility_produce")
    prevDate = newDate
    col += 1

    Else

    If row = 0 Then
    col += 1
    table.Rows.Add(tableRetrieve("sentDate"))
    table.Rows(row)(col) = tableRetrieve("facility_produce")
    prevDate = newDate
    col += 1
    Else
    col = 0
    row += 1
    table.Rows.Add(tableRetrieve("sentDate"))
    table.Rows(row)(col) = tableRetrieve("facility_produce")
    col += 1
    End If

    End If

  End If

Loop

dgvFacility.DataSource = table

对于每个循环,您在新行中写入 tableRetrieve("facility_produce")。您应该将其写入下一列。

Dim col As Integer = 0    
Do While tableRetrieve.Read = True

table.Columns.Add(tableRetrieve("facility"), Type.GetType("System.String"))

Dim newDate As DateTime = DateTime.ParseExact(dtpFrom.Value, "MM/dd/yyyy h:m:s tt",
  System.Globalization.DateTimeFormatInfo.InvariantInfo)
Dim myDate As String = newDate.ToString("yyyy-MM-dd", System.Globalization.DateTimeFormatInfo.InvariantInfo)

If myDate = tableRetrieve("sentDate") Then

If tableRetrieve("prod_id") = tableRetrieve("fac_id") Then
  If col = 0 Then
     table.Rows.Add(tableRetrieve("facility_produce"))
  Else
     table.Rows(0)(col) = tableRetrieve("facility_produce")
  End If
  col += 1
End If

End If

Loop

要事第一。我在表格的开头添加了 Dim countColumns As Integer 以便稍后调用以计算列数。

我在显示空白行 datagridview 但其中包含列 headers 的部分插入了 countColumns += 1。在数据库中注册的列已经计算在内。所以我以后可以用它来调用列数。

然后我放置了 table.Columns.Count <= countColumns 以便列数随着列的显示而增加,然后一旦数据库中注册的列数相等就停止添加列,以避免重复错误。

这是我的程序的一部分,我在其中显示列 headers 和行值:

Try
SQLConnection.Open()
tableRetrieve = sqlCommand.ExecuteReader

table.Columns.Add("Date", Type.GetType("System.String"))

Do While tableRetrieve.Read = True

    If table.Columns.Count <= countColumns Then

        table.Columns.Add(tableRetrieve("facility"), Type.GetType("System.String"))

    End If

    newDate = tableRetrieve("sentDate")


    If prevDate = newDate Then

        table.Rows(row)(col) = tableRetrieve("facility_produce")
        prevDate = newDate
        col += 1

    Else

        If col = 0 Then

            col += 1
            table.Rows.Add(tableRetrieve("sentDate"))
            table.Rows(row)(col) = tableRetrieve("facility_produce")
            prevDate = newDate
            col += 1

        Else

            col = 1
            row += 1
            table.Rows.Add(tableRetrieve("sentDate"))
            table.Rows(row)(col) = tableRetrieve("facility_produce")
            prevDate = newDate
            col += 1

        End If

    End If

Loop

dgvFacility.DataSource = table
tableRetrieve.Close()
sqlCommand.ExecuteNonQuery()
Catch ex As MySqlException
MsgBox(ex.Message.ToString)
Finally
SQLConnection.Close()
End Try

程序的这一部分通过以下代码检查日期是否相同:prevDate = newDate,如果相同则程序将添加 facility_produce在对应列的同一行。如果日期不相同,则程序检查 col 是否为零,如果为零,则 datagridview 仍然为空,程序将输入第一行及其产生的数量。如果 col 不为零,这意味着已经有一个 new date 并且已经有一个现有的行然后程序必须输入下一行以下值的数量。

If prevDate = newDate Then

    table.Rows(row)(col) = tableRetrieve("facility_produce")
    prevDate = newDate
    col += 1

Else

    If col = 0 Then

        col += 1
        table.Rows.Add(tableRetrieve("sentDate"))
        table.Rows(row)(col) = tableRetrieve("facility_produce")
        prevDate = newDate
        col += 1

    Else

        col = 1
        row += 1
        table.Rows.Add(tableRetrieve("sentDate"))
        table.Rows(row)(col) = tableRetrieve("facility_produce")
        prevDate = newDate
        col += 1

    End If

End If

抱歉这么长 post,但我希望这能帮助和指导任何使用内部联接然后将行值用作列 headers 的人。我很难解决这个问题。自 2010 年以来就没有使用过 VB 编程。早在 2010 年我还在使用 VB 6 方式,现在我对 VB.Net 已经非常过时了。有很多东西要学,我很享受。

感谢@milda 的帮助。您的代码指导我将我的价值观放在正确的位置。