从未绑定到datagridview的数据集向数据网格视图添加行

时间:2012-10-22 16:33:19

标签: vb.net datagridview

我有一个包含3行的datagridview(DGV)。我想将MySQL查询的结果(4行)添加到DGV。查询的结果放在DataSet(DS)中。将四个DS行添加到DGV的语法是什么?

1 个答案:

答案 0 :(得分:0)

您必须通过循环来添加数据集中的行。

Dim myConnObj As New MySqlConnection
Dim myDataSet As New DataSet
Dim myDataAdapter As New MySqlDataAdapter
Dim myCommand As New MySqlCommand

myCommand = New MySqlCommand("Your Query", "Your Connection")
myDataAdapter = New MySqlDataAdapter(myCommand)
myDataAdapter.Fill(myDataSet, "list")

With myDataSet
   For i as integer = 0 to .Tables(0).Rows.Count - 1
       DataGridView1.Rows.Add(.Tables(0).Rows(i).Item(0).ToString(), .Tables(0).Rows(i).Item(0).ToString())

'.Rows.Add(Column1, Column2, etc...) the number of column you add should match the column count of your DataGridView

   Next
End With