3

Alright, this probably already has a thread somewhere( which if someone knows of, feel free to point me in that direction!), but a while of searching hasn't led me to it.

I'm currently using the datagrid viewer to display a table in my vb.net (VS 2013) program. I'm not using a database/sql setup (although there is a good number of tutorials online on that one). I look on my network for whatever devices I have connected. I'm going to get their IP address, firmware, time accessed, etc into a string.

like so:

Dim device_info as string() = {"webcam", "123.123.123.123", _
 "3/18/2014", "firmware version 2"} 

You get the idea.

So I'll have an array of strings about this device, and multiple devices in a row. Basically a table of each device and the corresponding info.

My issue is that I'm trying to display this info in data grid, and I can't quite it it right. I want each row to be a device, and each element of the device_info array of strings to be a cell. And possibly a column of checkboxes to select(a problem I can deal with later). But I'm having issues making a matrix or an array of arrays, and being able to display that into the gridviewer. Here's what I have so far (with junk strings inputted for the exercise):

Dim device1 As String() = {"Acti", "00:09:10", "SF0900", "FirmwareVersion 1.0"}
Dim device2 As String() = {"Sony", "03:45:h5", "KK5000", "Firm ware 8.0"}

Dim arraylist As New ArrayList
    arraylist.Add(device1)
    arraylist.Add(device2)

Dim dt As New DataTable
    dt.Columns.Add("Brand")
    dt.Columns.Add("Mac")
    dt.Columns.Add("Model")
    dt.Columns.Add("Firmware")

  For i As Integer = 0 To arraylist.Count - 1
        Dim dr As DataRow
        dr = dt.NewRow()
        For j As Integer = 0 To dt.Columns.Count - 1
            dr.Item(j) = arraylist(j)
        Next
        dt.Rows.Add(dr)

    Next

    dataGridViewer.DataSource = dt

This errors out in the build, mostly because of the arraylist(j) line. I really want to access arraylist(device(j)), but I'm unclear on how to do that. Is there a way to access an element of an array that is within an array list? Or am I going about this the whole wrong way and there is a more efficient method? I basically want to copy a matrix and import it into the grid viewer. Feeling like bit of a dummy here :D.

Thanks for the help!! -K

3 Answers 3

1

I typically try to avoid using arrays as I prefer lists. Below is a quick sample I put together that does what you are looking for but instead uses a list and a structure. I think you will find it slightly easier to follow than what you were trying to do with the arrays.

Structure device
    Dim brand As String
    Dim mac As String
    Dim model As String
    Dim firmware As String
End Structure

Dim listOfDevices As New List(Of device)

Public Sub UpdateDataGridView()

    Dim device1 As New device
    device1.brand = "Acti"
    device1.mac = "00:09:10"
    device1.model = "SF0900"
    device1.firmware = "FirmwareVersion 1.0"

    Dim device2 As New device
    device2.brand = "Sony"
    device2.mac = "03:45:h5"
    device2.model = "KK5000"
    device2.firmware = "Firm ware 8.0"

    listOfDevices.Add(device1)
    listOfDevices.Add(device2)

    For Each d As device In listOfDevices
        DataGridView1.Rows.Add(d.brand, d.mac, d.model, d.firmware)
    Next

End Sub

Keep in mind the columns for the DataGridView would already need to be created in order for the sample above to work.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried doing the structures, and I like how clean it is. The problem is I don't know the exact number of columns are going to be set just yet, but it is a nice solution.
1

I do belive this is a facepalm issue for you :)

Try this line instead:

dr.Item(j) = arraylist(i)(j)

i = Devices. j = CellValues.

1 Comment

0

I found this as a solution to our problem, the UbicBase(,) is passing a reference to multiple array becouse I'm using array instead of SQL ado connections (It is kinda an exercise and a way not to access the database to many times) the "tabla" was something I didn't use here so it is in the declaration but not in the implementation of the function.

The function is working from a "module" so it is filling the datagridview from "outside" the form, that's why it is calling it "Form4.DataGridView1.Rows.Insert((a - 1), arr)".

I'm using a one level array to pass the data from the "line" of the the 2 dimension array t the datagridview, if works with multiple sizes of arrays becouse "cantCamp" is getting the number of columns of the array from the declaration of the array in another FUNCTION. the "CantLines" is the amount of lines of the array and is being filled where the array is declared as "cantCamp".

Public Function cargarDataGrid(ByRef UbicBase(,) As String, ByVal tabla As String)
    'DataGridView1
    Form4.DataGridView1.Rows.Clear()
    Form4.DataGridView1.Columns.Clear()

    'Call buscarArchivos(tabla)
    If cantCamp <= 0 Then
        cantCamp = 2
    End If

    If cantLineas <= 0 Then
        cantLineas = 2
    End If
    For a As Integer = 0 To cantCamp
        Form4.DataGridView1.Columns.Add(UbicBase(0, a).ToString, UbicBase(0, a).ToString())
    Next
    Form4.DataGridView1.Rows.Add()
    Form4.Label1.Text = Form4.DataGridView1.Columns.Count.ToString()
    Form4.Label2.Text = Form4.DataGridView1.Rows.Count.ToString()
    For a As Integer = 0 To cantLineas
        Dim arr(cantCamp) As String
        For b As Integer = 0 To cantCamp
            'Form4.DataGridView1.SetBounds(int_a, int_b, 50, 50)
            arr(b) = UbicBase(a, b)
        Next
        If a > 0 Then
            Form4.DataGridView1.Rows.Insert((a - 1), arr)
        End If

    Next
End Function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.