如何用对象通用字典填充vb.net中datagridview的数据源(见图)
我尝试使用图片中的结构填充datagridview或simular来显示数据。我不知道怎么做。说明(c#)告诉我如何使用: var transHistory=btceApi.GetTransHistory(); 据我所知,它将list()作为字典返回(对于整数,事务) (公共历史课) 我这样翻译:Dim trnh As object=BtceApi.GetTransHistory(计数:=20) 它实际上返回了20条记录,见图。 但是如何绑定(?)并从此显示数据?如何用对象通用字典填充vb.net中datagridview的数据源(见图),.net,vb.net,.net,Vb.net,我尝试使用图片中的结构填充datagridview或simular来显示数据。我不知道怎么做。说明(c#)告诉我如何使用: var transHistory=btceApi.GetTransHistory(); 据我所知,它将list()作为字典返回(对于整数,事务) (公共历史课) 我这样翻译:Dim trnh As object=BtceApi.GetTransHistory(计数:=20) 它实际上返回了20条记录,见图。 但是如何绑定(?)并从此显示数据? 感谢您的回答。首先创建data
感谢您的回答。首先创建datagrid视图(如果尚未创建) 然后设置其属性(其中一些是可选的) 如果要清除网格,可以使用以下方法:
' Clear everything out of the data grid
' -------------------------------------
With data_grid
While .RowCount > 0
.Rows.Clear()
End While
While .ColumnCount > 0
.Columns.Clear()
End While
End With
将柱添加到网格,如下所示:
' Add the column headers to the columns
' -------------------------------------
Dim col_0 As New DataGridViewTextBoxColumn
col_0.Name = "key"
data_grid.Columns.Add(col_0)
Dim col_1 As New DataGridViewTextBoxColumn
col_1.Name = "Name"
col_1.FillWeight = 100
col_1.ReadOnly = True 'optional
data_grid.Columns.Add(col_1)
Dim col_2 As New DataGridViewTextBoxColumn
col_2.Name = "Address"
col_2.FillWeight = 70
col_2.ReadOnly = True 'optional
data_grid.Columns.Add(col_2)
' Add the data to the columns
' ---------------------------
For r As Integer = 0 To grid_data_array.Count() - 1
data_grid.Item(0, r).Value = grid_data_array(r)(0) ' key
data_grid.Item(1, r).Value = grid_data_array(r)(1) ' name
data_grid.Item(2, r).Value = grid_data_array(r)(2) ' address
Next
现在已经添加了列,需要填充它们
首先,需要添加要使用的所有行。这是你的记录集中的记录数
data_grid.Rows.Add(grid_data_array.Count()) ' in your case grid_data_array is your collection
然后将数据添加到如下列:
' Add the column headers to the columns
' -------------------------------------
Dim col_0 As New DataGridViewTextBoxColumn
col_0.Name = "key"
data_grid.Columns.Add(col_0)
Dim col_1 As New DataGridViewTextBoxColumn
col_1.Name = "Name"
col_1.FillWeight = 100
col_1.ReadOnly = True 'optional
data_grid.Columns.Add(col_1)
Dim col_2 As New DataGridViewTextBoxColumn
col_2.Name = "Address"
col_2.FillWeight = 70
col_2.ReadOnly = True 'optional
data_grid.Columns.Add(col_2)
' Add the data to the columns
' ---------------------------
For r As Integer = 0 To grid_data_array.Count() - 1
data_grid.Item(0, r).Value = grid_data_array(r)(0) ' key
data_grid.Item(1, r).Value = grid_data_array(r)(1) ' name
data_grid.Item(2, r).Value = grid_data_array(r)(2) ' address
Next
我的数组有两个维度,你的可能只有一个维度
现在,我设置了最终参数。其中一些是可选的。我将网格的高度设置为400像素。如果超过这个值,我会添加滚动条并保持在400。如果小于,则没有滚动条。我还将col.0设置为hidden,因为我不希望人们看到该行的键
With data_grid
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
.RowHeadersVisible = False ' remove the first column of headers
.AllowUserToAddRows = False ' remove the last row from the list that shows up when adding the vertical scrollbar
.AllowUserToDeleteRows = False
.AllowUserToOrderColumns = True
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.MultiSelect = False
.AllowUserToResizeRows = False
.Columns(0).Visible = False 'hide the (key) column
' if there are more rows than space available, then add a scoll bar
' -----------------------------------------------------------------
If ((data_grid.RowCount * 24) + 25) > Me.Height - 400 Then
.ScrollBars = ScrollBars.Vertical
data_grid.Height = Me.Height - 400
Else
' just set the height to equal the height of the rows with no scroll bar
' ----------------------------------------------------------------------
.Height = (.RowCount * 24) + 25
.ScrollBars = ScrollBars.None
End If
End With
- 行的默认高度为24像素,标题行的默认高度为25像素。这就是为什么我使用这些数字来计算网格的大小
就这样。您愿意学习如何使用代码手动执行,还是只需要一个简单的绑定解决方案?我想知道代码。谢谢您的评论,我已经尝试过了。我在For r As Integer=0 To trnh.Count-1>找不到TransHistory类型的公共成员计数时遇到错误。能否遍历20条记录并将它们放入数组中?trnh需要是一个集合。它可以是一个数组或arraylist,也可以只是一个列表,但它需要一个计数。我希望它可以用一种更快、更简单的方式完成,在一个语句中,但我想我会这样做。这是最好的办法吗?对于trnh.列表中的每个事务,事务Dim v1为整数=。键Dim v2为事务=。值…=v2.金额…=v2.货币等以NextI结束。我不确定您所说的最佳方式是什么意思。但您只需要以您能(或知道如何)的最佳方式遍历集合,并将值放入列和行中。