我创建了一个包含 4 列的 Datagridview,EJ:
ID, Name, Quantity, other
但我想从 MySQL EJ 中填充这 3 列:
item_id, item_name, item_quantity
我试过这段代码:
Using cn As New MySqlConnection("server=10.10.2.1;userid=root;password=gf-159753;database=quick_admon")
cn.Open()
Dim da As New MySqlDataAdapter("SELECT * from qa_items", cn)
' DataTable
Dim dt As New DataTable
' llenar el DataTable
da.Fill(dt)
' enlazar el DataTable al BindingSource
list_items.DataSource = dt
With list_items
.MultiSelect = False
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.DataSource = list_items.DataSource
End With
End Using
但这会创建新列并且不会写入现有列,花了一段时间寻找解决方案但只找到这样的方法。
最佳答案
您未能将查询中的列绑定(bind)
到数据 GridView 列中。为此,
1.) Right Click DataGridView.
2.) A Popup Menu appears and Click Edit Columns
3.) Bind each columns (ID, Name, Quantity, other) by typing the field name (item_id, item_name, item_quantity) respectively from your query in the DataPropertyName property (so that it will not create another column like you did).
大功告成!
更新
以编程方式设置DataPropertyName
list_items.Columns("ID").DataPropertyName = "item_id"
或假设 ID 是您的第一列:
list_items.Columns(0).DataPropertyName = "item_id"
关于mysql - 从 MySQL 数据 VB.NET 填充 datagridview 中的特定列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9154130/