【问题标题】:Join 2 tables, add first table as column headers for datagridview then add second table as rows below column headers加入 2 个表,将第一个表添加为 datagridview 的列标题,然后将第二个表添加为列标题下方的行
【发布时间】:2020-11-26 10:19:23
【问题描述】:

我想为 FACILITIES 添加列,然后使用他们的 fac_id 从另一个表中查找他们的产水量,以在列标题下方显示为单行。

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

我可以添加列标题,但在行中失败。

下面是输出的图像。显示列标题,但产水量仅显示在一行中。基于 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

【问题讨论】:

    标签: mysql vb.net datatable datagridview inner-join


    【解决方案1】:

    对于每个循环,您在新行中写入 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
    

    【讨论】:

    • 非常感谢!奇迹般有效!我不知道你可以像这样对 table.rows 进行编码。谷歌搜索了这么多,没有这样的示例代码。你真是个天才! :)
    • 我尝试为新值添加行,但我收到一个错误,即未找到第 1 列或第 2 列或第 3 列。 :( 我也收到了列名重复的错误。
    • 您应该只在处理第一行时添加列(table.Columns.Add...),然后列准备好用于所有行。
    • 我做了内部连接,我只能在 Do while tableRetrieve.Read = True 之后添加列。即使表是内部连接的,有什么方法可以添加列?
    • 我也收到错误 System.IndexOutOfRangeException: 'Cannot find column 2.'当我尝试添加 if 语句来检查列名是否存在时。
    【解决方案2】:

    首先要做的事情。我在表单的开头添加了Dim countColumns As Integer,以便稍后调用以计算列数。

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

    然后我放置了table.Columns.Count <= countColumns,这样列数会随着列的显示而增加,然后在数据库中注册的列数相等时停止添加列,以避免重复出现错误。

    这是我的程序中显示列标题和行值的部分:

    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 不为零,则表示已经有一个新日期,并且已经存在一个行,那么程序必须在下面输入下一行值。 p>

    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

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

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

    【讨论】:

    • 哦,是的,你自己搞定了!这是学习它的最佳方式。
    • 非常感谢!希望我的代码也能帮助其他编码人员。 :)
    猜你喜欢
    • 1970-01-01
    • 2014-05-26
    • 2013-10-23
    • 1970-01-01
    • 2023-01-28
    • 1970-01-01
    • 2013-02-23
    • 2011-03-30
    • 2011-10-09
    相关资源
    最近更新 更多