filling datagridview from an array

Asked by At

How can I fill a datagridview from an array or a list.

I have the following line that get the like in a datagridview from a textbox and adds it to an array, and I want to fill a datagridview with these values. how can I do that.

 Private Sub TextBox4_TextChanged(sender As Object, e As EventArgs) Handles TextBox4.TextChanged


        Dim match As DataGridViewCell() = (From row As DataGridViewRow In Me.DataGridView2.Rows From cell As DataGridViewCell In row.Cells Select cell Where CStr(cell.FormattedValue).Contains(Me.TextBox4.Text)).ToArray()

        DataGridView2.DataSource = match.ToList()

end sub 

tried this and it didnt work...

1 Answers

0
Community On

As I understand, here you me humble example to do that. But remember that we have an array which consists of five element "Arr(5)" with some values, then add these values in a specific DGV column, so please do the appropriate modification for your exact need.

Here is the code:

Public Class Form1

Private Sub BtnFillDGVFromArray_Click(sender As Object, e As EventArgs) Handles BtnFillDGVFromArray.Click

    Dim Arr(5) As String

    ' This to give the array initial values to aviod the null reference
    For i = 0 To 4
        Arr(i) = Nothing
    Next

    ' Add some strings to the array
    For i = 0 To 4
        Arr(i) = "ArrayVar(" & i & ")"
    Next

    DGV.Columns.Add("Col1", "Col1")
    For i = 0 To UBound(Arr) - 1
        DGV.Rows.Add(Arr(i).ToString)
    Next

End Sub

End Class

enter image description here