We help IT Professionals succeed at work.

Display list contents in gridview

I'm trying to get each list item into a table row and then bound to a gridview.

When i set a breakpoint at prefFlrIP = (From q In NewArray Select q) I can see the data i want displayed is present in prefFlrIP but only the first list item is displayed in my gridview.
Dim Table1 As DataTable
        Table1 = New DataTable("SnapDrivePrefIP")

        Dim PrefIP As DataColumn = New DataColumn("Preferred Filer/IP")
        PrefIP.DataType = System.Type.GetType("System.String")
        Table1.Columns.Add(PrefIP)


        Dim Converter = Encoding.Convert(Encoding.Unicode, Encoding.Unicode, IO.File.ReadAllBytes(txtPath.Text & "\SnapDriveInfo\SnapDrive_RegistryInfo.log"))
        Dim ConvertedFile = System.Text.Encoding.Unicode.GetString(CType(Converter, Byte())).Split(vbCrLf)
        Dim sdRegistryData = (From j In ConvertedFile Where Not String.IsNullOrEmpty(j.Trim) Select j.Replace(" ", String.Empty).Trim).ToList

        Dim ReqCK = "[SYSTEM\CurrentControlSet\Services\SWSvc\Parameters\PreferredFilerIPAddresses]"
        Dim LastOC = "[SYSTEM\CurrentControlSet\Services\SWSvc\Parameters\Protocols]"
        Dim prefFlrIp As New List(Of String)

        If sdRegistryData.Contains(ReqCK) Then
            Dim NewArray As New List(Of String), StartLog As Boolean = False
            For Each x In sdRegistryData
                If x = LastOC Then Exit For
                If StartLog Then NewArray.Add(x)
                If x = ReqCK Then StartLog = True
            Next
            If NewArray.Count > 0 Then
                prefFlrIP = (From q In NewArray Select q).ToList
            End If




            Dim Row1 As DataRow
            Try
                Row1 = Table1.NewRow()

                Row1.Item(PrefIP) = If(prefFlrIp.Count > 0, prefFlrIp(0), "Not Set")

                Table1.Rows.Add(Row1)
            Catch ex As Exception

            End Try
        End If

        gridPrefIP.DataSource = Table1
        gridPrefIP.DataBind()

Open in new window

Comment
Watch Question

Commented:
You are only getting the first list item, because that is the only item you are adding into the table that is the data source. So you'd want something like the following:

(I don't expect it's exactly right, and you probably could make it more efficient using the first for each, rather than creating another array first)

 
         Dim NewArray As New List(Of String), StartLog As Boolean = False

            For Each x In sdRegistryData
                If x = LastOC Then Exit For
                If StartLog Then NewArray.Add(x)
                If x = ReqCK Then StartLog = True
            Next
            If NewArray.Count > 0 Then
                prefFlrIP = (From q In NewArray Select q).ToList
            End If
            Dim Row1 As DataRow
dim count as integer = 0
            Try
for each listitem in prefFlrIP 
                Row1 = Table1.NewRow()
                Row1.Item(count) = listitem

                Table1.Rows.Add(Row1)
                count += 1
next
            Catch ex As Exception

            End Try

Open in new window

Author

Commented:
Thank you for putting me down the right path. Your code still only returned one line but you got me pointed in the right direction. Thanks!

 
            If NewArray.Count > 0 Then
                prefFlrIp = (From q In NewArray Select q).ToList
            End If

            Dim Row1 As DataRow
            Try
                For Each ListItem In prefFlrIp
                    Row1 = Table1.NewRow()
                    Row1.Item(PrefIP) = If(prefFlrIp.Count > 0, ListItem, "Not Set")
                    Table1.Rows.Add(Row1)
                Next
            Catch ex As Exception

            End Try
        End If

        gridPrefIP.DataSource = Table1
        gridPrefIP.DataBind()

Open in new window

Commented:
For Each ListItem In prefFlrIp
    Row1 = Table1.NewRow()
    Row1.Item(PrefIP) = ListItem
    Table1.Rows.Add(Row1)
Next

Open in new window

Commented:
or for a different approach (having BOTH items listed in the grid)
Dim xGrid As DataGridViewComboBoxColumn = New DataGridViewComboBoxColumn
Me.DataGridView1.Columns.Add(xGrid)
Dim ConvertedFile = File.ReadAllLines(txtPath.Text & "\SnapDriveInfo\SnapDrive_RegistryInfo.log", Encoding.Unicode)
Dim sdRegistryData = (From j In ConvertedFile Where Not String.IsNullOrEmpty(j.Trim) Select j.Trim).ToList
Dim ReqCK = "[SYSTEM\CurrentControlSet\Services\SWSvc\Parameters\PreferredFilerIPAddresses]"
Dim LastOC = "[SYSTEM\CurrentControlSet\Services\SWSvc\Parameters\Protocols]"
Dim prefFlrIp As New List(Of String)

If sdRegistryData.Contains(ReqCK) Then
    Dim StartLog As Boolean = False
    For Each x In sdRegistryData
        If x = LastOC Then Exit For
        If StartLog Then prefFlrIp.Add(x.Replace(vbTab, " "))
        If x = ReqCK Then StartLog = True
    Next
    Try
        Dim xRow As DataGridViewComboBoxCell = New DataGridViewComboBoxCell With {.DataSource = If(prefFlrIp.Count > 0, prefFlrIp.ToArray, {"Not Set"})}
        Me.DataGridView1.Rows.Add()
        Me.DataGridView1.Item(0, Me.DataGridView1.RowCount - 1) = xRow
    Catch ex As Exception

    End Try
End If

Open in new window

For this to work,make sure you set the datagridview property of AllowUsersToAddRows to False. Then you'll have the items in a combobox

Explore More ContentExplore courses, solutions, and other research materials related to this topic.