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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9154130/
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-08-31 12:01:58  来源:igfitidea点击:

Fill specific columns in datagridview from MySQL data VB.NET

mysqlvb.netdatagridview

提问by John Nu?ez

I have created a Datagridview with 4 Columns, EJ:

我创建了一个带有 4 列的 Datagridview,EJ:

ID, Name, Quantity, other

But i want to fill these 3 Columns from MySQL EJ:

但我想从 MySQL EJ 填充这 3 列:

item_id, item_name, item_quantity

I tryed this code:

我试过这个代码:

    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

but this creates new columns and does not write to the existing, took a while looking for a solution but only find methods like this.

但这会创建新列并且不会写入现有列,花了一段时间寻找解决方案但只找到这样的方法。

回答by John Woo

You failed to bindthe columns from your query into your datagrid view columns. To do this,

您未能将bind查询中的列转换为数据网格视图列。去做这个,

1.) Right ClickDataGridView.
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 DataPropertyNameproperty (so that it will not create another column like you did).

1.)右键单击DataGridView。
2.) 出现一个弹出菜单并单击编辑列
3.)通过在DataPropertyName属性中分别键入查询中的字段名称(item_id、item_name、item_quantity)来绑定每一列(IDNameQuantityother)(以便它不会像您那样创建另一列)。

And you're done!

你完成了!

UPDATE

更新

Setting DataPropertyNamePropgramatically

以编程方式设置DataPropertyName

list_items.Columns("ID").DataPropertyName = "item_id"

or Assuming that ID is your first column:

或假设 ID 是您的第一列:

list_items.Columns(0).DataPropertyName = "item_id"