从列表框或设置集合中填充datagridview

时间:2014-05-07 09:38:58

标签: vb.net datagridview listbox

我的表单中有listbox(与设置相关的数据)和datagridview。我想从listbox中填充datagridview中的第0列。无法在网络上找到解决方案,所以我试过这个:

            For i As Integer = 0 To ListBox1.Items.Count - 1

            DataGridView1.Rows(i).Cells(0).Value = ListBox1.Items

        Next

结果只是第0列中的一个项目:" System.Windows.Forms.ListBox + ObjectCollection"

显然这与设置(保存列表框中的数据)有关,而且我不知道如何修复它。

P.S。上面的代码也会从列表框中删除项目!?

编辑:

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    TextBox1.Visible = False
    For Each item As String In My.Settings.Setting
        ListBox1.Items.Add(item.ToString)
        DataGridView1.Columns.Add(("BNK"), "Bank")
        DataGridView1.Columns.Add(("ZR"), "2410")
        DataGridView1.RowHeadersVisible = False
        DataGridView1.Columns(0).Width = 55
        DataGridView1.Columns(0).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft
        DataGridView1.Columns(1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft

        For i As Integer = 0 To ListBox1.Items.Count - 1
            Dim dr As New DataGridViewRow
            DataGridView1.Rows.Add(dr)
            DataGridView1.Rows(i).Cells(0).Value = ListBox1.Items(i)
            DataGridView1.AllowUserToAddRows = False
        Next
    Next
End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    If Button3.Text = "Add a bank" Then
        TextBox1.Visible = True
        TextBox1.Text = ""
        TextBox1.Focus()
        Button3.Text = "Confirm"
    ElseIf Button3.Text = "Confirm" Then

        TextBox1.Visible = False
        Button3.Text = "Add a bank"
        Dim newURL As String = TextBox1.Text.ToString

        ListBox1.Items.Add(TextBox1.Text.ToString) 'add to ListBox1
        My.Settings.Setting.Add(TextBox1.Text.ToString) 'add to My.Settings.favList


    End If
End Sub

3 个答案:

答案 0 :(得分:1)

试试这段代码。它的工作原理

Private Sub Form_Load()
    dim cmd as SqlCommand("Select col1 from tblname",conn)
    dim da as new SqlDataAdapter(cmd)
    dim ds as new dataset
    da.fill(ds)
    Listbox1.datasource=ds
    ListBox1.DisplayMember = "col1"
    ListBox1.ValueMember = "col1"
    For i As Integer = 0 To ListBox1.Items.Count - 1
        Dim dr As New DataGridViewRow
        DataGridView1.Rows.Add(dr)
        DataGridView1.Rows(i).Cells(0).Value = ListBox1.Items(i)
        DataGridView1.AllowUserToAddRows = False
    Next
End Sub


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
dim cmd as new SqlCommand("insert into tblname values('" & textbox1.Text & "')",conn)
cmd.ExecuteNonQuery()
ListBox1.Items.Add(TextBox1.Text)
    For i As Integer = 0 To ListBox1.Items.Count - 1
        Dim dr As New DataGridViewRow
        DataGridView1.Rows.Add(dr)
        DataGridView1.Rows(i).Cells(0).Value = ListBox1.Items(i)
        DataGridView1.AllowUserToAddRows = False
    Next
End Sub

答案 1 :(得分:0)

这会欺骗......

For Count = 0 To ListBox1.Items.Count -1 
   DataGridView1.Rows(i).Cells(Count).Value = ListBox1.Items(Count)
Next

答案 2 :(得分:0)

    For i As Integer = 0 To ListBox1.Items.Count - 1

        DataGridView1.Rows(i).Cells(0).Value = ListBox1.Items(i)

    Next