I have created a Datagridview with 4 Columns, EJ:
ID, Name, Quantity, other
But i want to fill these 3 Columns from MySQL EJ:
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.
解决方案
You failed to bind the columns from your query into your datagrid view columns. To do this,
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).
And you're done!
UPDATE
Setting DataPropertyName Propgramatically
list_items.Columns("ID").DataPropertyName = "item_id"
or Assuming that ID is your first column:
list_items.Columns(0).DataPropertyName = "item_id"