VB.NET DataGridView - >添加列并使用其他单元格中的数据填充单元格

时间:2013-11-21 20:34:00

标签: mysql vb.net datagridview

我有一个DataGridView,我按以下方式填写:

Function updategrid(ByVal CurrentBroker, ByVal CurrentPallet)
    Dim i = 0
    Dim SQLText As String
    SQLText = "SELECT " & _
    "invoer.idInvoer," & _
    "invoer.BRKNo as Broker," & _
    "invoer.Palletno as Pallet," & _
    "invoer.Tabblad AS ValTabblad," & _
    "Tabblad.TabbladType, " & _
    "invoer.Hardwaretype as Hardwaretype," & _
    "invoer.Fabrikant AS Fabrikant," & _
    "invoer.Model As Model, " & _
    "invoer.Serienummer, " & _
    "invoer.AssetTag, " & _
    "invoer.Schade, " & _
    "invoer.Opmerkingen, " & _
    "Tabblad.idTabblad, " & _
    "invoer.details, " & _
    "invoer.aantal, " & _
    "invoer.RegisterDate " & _
    "FROM Invoer as invoer " & _
    "INNER JOIN Tabblad as Tabblad ON invoer.Tabblad = tabblad.idTabblad " & _
    "WHERE invoer.BRKNo = '" & CurrentBroker & "'" & _
    "AND invoer.PalletNo = " & CurrentPallet & "  " & _
    "ORDER BY invoer.RegisterDate DESC"

    ds.Tables.Clear()
    Try
        Data = New DataTable
        dataAdap = New MySqlDataAdapter(SQLText, dbconn)

        cmdBuilder = New MySqlCommandBuilder(dataAdap)
        dataAdap.Fill(ds, "Overzicht")
        With DGVOverzicht
            .DataSource = ds.Tables("Overzicht")
            .ColumnHeadersVisible = True
            .AutoGenerateColumns = True
            .Visible = True

            For i = 0 To .ColumnCount - 1 Step 1
                Select Case .Columns(i).HeaderText
                    Case "ValTabblad" : .Columns(i).Visible = False
                    Case "details"
                        ' hide the ID's, display text..
                        .Columns(i).Visible = False
                    Case "idTabblad" : .Columns(i).Visible = False
                    Case "idHWModel" : .Columns(i).Visible = False
                    Case "idHardware" : .Columns(i).Visible = False
                    Case "idInvoer" : .Columns(i).Visible = False
                End Select
            Next
        End With
        Return True
    Catch ex As MySqlException
        MsgBox(ex.Message)
        Return False
    End Try

    Return 1
End Function

这就像一个魅力,但我需要在我隐藏的列旁边添加一列:详细信息。 列详细信息保存记录中选择的所有详细信息的ID(例如1; 5; 2; 19; 20; 100) 我想向用户提供有关实际所选细节的更多信息,例如通过在MySQL中查找以查看这些ID实际上是什么细节..

然而,我被告知不要在MySQL中以逗号分隔的列表进行内部联接,因为它会像“冻结的蜗牛一样慢”。 所以我能想到的下一件事就是用数据库中的所有信息填充DGV,隐藏ID列并“查找并替换”另一个单元格中的值,从而在MySQL中查找它。

如果有人能指出我在这方面的某种方向,我会非常感激..因为我现在盯着代码几个小时了,似乎没有一个灯泡弹出:)

提前感谢!

1 个答案:

答案 0 :(得分:0)


嗯,我觉得我终于做到了。 我所做的是在SQLstatemenr中选择一个额外的列(invoer.idInvoer AS details_explain),这样我就可以使用额外的列并在我想要的位置。 然后我添加了以下代码来更改该列的内容:

    ' replace ID's in Details_Explain with ShortCodes from DGV

    For row = 0 To DGVOverzicht.Rows.Count - 1
        Dim strDetailsExplain = ""
        Dim DetailIDs As Array
        DetailIDs = Split(DGVOverzicht.Rows(row).Cells("details").Value.ToString, ";")
        For Each item In DetailIDs
            If Not item = Nothing Then
                strDetailsExplain = strDetailsExplain & DGV.Rows(Int(item)).Cells("DetailsCode").Value & ":"
            End If
        Next

        DGVOverzicht.Rows(row).Cells("Details_Explain").Value = strDetailsExplain
    Next

我认为这不是最干净的方法,但它必须这样做,因为我不知道更好的方法。