DataGrid通过ForEach创建视图

时间:2012-04-18 19:34:05

标签: vb.net datagridview dataset

我在从名为“dt”的名为“dt”的表中的“MobileID”列中的每一行中提取值时遇到问题。

以下是数据集和数据表的创建方式。

        Public Function GetClientSpecificDevices(ByVal argHost As FOO.BAR.SHOO, ByVal currentClientIDSelection As Integer) As DataSet        
        Return argHost.GetDataSet("SELECT MobileID FROM mobiledevices WHERE ClientID = " & currentClientIDSelection & ";")

        'create dataset to handle tabular data
        Dim ds As New DataSet
        'Ensure every call refreshes dataset rather than appending
        'ds.Tables.Clear()
        Dim dt As New DataTable

        'add table and populate table with data
        ds.Tables.Add(dt)
        dt.Columns.Add("MobileID")
        'return dataset
        Return ds

对于DataGrid

Function GenerateDataRows() As Object
    Dim row As New DataGridViewRow()
    Dim ds As DataSet = GetClientSpecificDevices(_objHost, CInt(comboBox_clients.ValueMember))
    If Not ds Is Nothing AndAlso ds.Tables(0).Rows.Count > 0 Then
        For Each A As DataRow In ds.Tables(0).Rows
            'populate rows
            Dim deviceID As New DataGridViewTextBoxCell
            Dim deviceName As New DataGridViewTextBoxCell
            Dim deviceModel As New DataGridViewTextBoxCell
            Dim devicePhoneNum As New DataGridViewTextBoxCell
        Next
    End If

    Return row
End Function

我正在寻找类似的东西

Dim deviceID As New DataGridViewTextBoxCell
deviceID.Value  = A.MobileID

但它没有用。

1 个答案:

答案 0 :(得分:0)

似乎您正在创建一个单独的DataGridViewTextBoxCell,它不与您的DataGridViewRow合作,您可以尝试以下代码

Function GenerateDataRows() As Object
    Dim row As New DataGridViewRow()
    Dim ds As DataSet = GetClientSpecificDevices(_objHost, CInt(comboBox_clients.ValueMember))
    If Not ds Is Nothing AndAlso ds.Tables(0).Rows.Count > 0 Then
        For Each A As DataRow In ds.Tables(0).Rows
            'populate rows
           row.cell(0).value = A.MobileID
        Next
    End If

    Return row
End Function