将行添加到绑定的datagridview1

时间:2013-04-15 01:43:53

标签: vb.net

我有一个datagridview1,它通过BindingSource数据绑定到数据集

dim dt as datatable
dim bsData as bindingsource

bsData.DataSource = dt
DataGridView1.DataSource = bsData

我还有来自另一个数据表dt5的行,我需要添加到datagridview1数据表中

我的代码在datagridview1中创建一个名为Objectives的新列,但它不会从dt5

添加行值(H1)

任何想法?理解

Dim dt5 As New DataTable
        dt5.Load(cmd.ExecuteReader)
        Dim mybindingsource As BindingSource = CType(DataGridView1.DataSource, BindingSource)
        Dim dtts As DataTable = CType(mybindingsource.DataSource, DataTable)
        dtts.Columns.Add("Objectives")
        Dim newrow As DataRow
        For i = 0 To (dt5.DefaultView.Count - 1)
            newrow = dtts.NewRow

            newrow.Item("Objectives") = dt5.Rows.Item(i).Item("H1")
            dtts.Rows.Add(newrow)
        next

0 个答案:

没有答案