mysql - 从 MySQL 数据 VB.NET 填充 datagridview 中的特定列

标签 mysql vb.net datagridview

我创建了一个包含 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/

相关文章:

php - 在 MySql 中插入唯一的 100 万条记录

mysql - 如何在mysql中找到Unicode字符?

c# - 找不到按钮调用的方法

c# - gridview 列的总和,跳过重复项

.net - DataGridView 对 DateTime 列中的空值进行排序

postgresql - 从另一行更新postgres中的一行

mysql - 从两个以上的表中检索数据

c# - 记录操作延迟

c# - 根据分数和校园将学生分组 - ASP.NET、VB 和 SQL

vb.net - 使用SQLiteDataAdapter与SQLiteDataReader填充DataGridView的差异