新手问题:VB.net 数组到 datagridviewer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22482875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:09:06  来源:igfitidea点击:

Newbie prob: VB.net array of arrays into datagridviewer

arraysvb.netdatagridview

提问by Kat

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 notusing 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.

我目前正在使用数据网格查看器在我的 vb.net (VS 2013) 程序中显示一个表格。我没有使用数据库/sql 设置(尽管网上有很多关于该设置的教程)。我在我的网络上查找我连接的任何设备。我将把他们的 IP 地址、固件、访问时间等放入一个字符串中。

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):

我的问题是我试图在数据网格中显示这些信息,但我不能完全正确。我希望每一行都是一个设备,并且 device_info 字符串数组的每个元素都是一个单元格。可能还有一列要选择的复选框(我以后可以处理的问题)。但是我在制作矩阵或数组数组时遇到了问题,并且无法将其显示到 gridviewer 中。这是我到目前为止的内容(为练习输入了垃圾字符串):

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.

这在构建中出错,主要是因为 arraylist(j) 行。我真的很想访问 arraylist(device(j)),但我不清楚如何做到这一点。有没有办法访问数组列表中的数组元素?或者我是否以完全错误的方式解决这个问题并且有更有效的方法?我基本上想复制一个矩阵并将其导入网格查看器。感觉这里有点假:D。

Thanks for the help!! -K

谢谢您的帮助!!-K

采纳答案by Force

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.

请记住,已经需要创建 DataGridView 的列才能使上述示例正常工作。

回答by WozzeC

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

我相信这对你来说是一个面部问题:)

Try this line instead:

试试这一行:

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

i = Devices. j = CellValues.

i = 设备。j = 单元格值。

回答by UnknownSoldier78

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.

我发现这是我们问题的解决方案,UbicBase(,) 正在传递对多个数组的引用,因为我使用的是数组而不是 SQL ado 连接(这是一种练习,也是一种不多次访问数据库的方法) “tabla”是我在这里没有使用的东西,所以它在声明中而不是在函数的实现中。

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)".

该函数是从“模块”工作的,因此它从表单的“外部”填充 datagridview,这就是为什么它称之为“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".

我正在使用一级数组将来自二维数组的“行”的数据传递给 datagridview,如果适用于多种大小的数组,因为“cantCamp”正在从声明中获取数组的列数另一个 FUNCTION 中的数组。“CantLines”是数组的行数,在数组被声明为“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