vb.net 从列表框或设置集合填充 datagridview

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

Fill datagridview from listbox or settings collection

vb.netdatagridviewlistbox

提问by Jovica

There are listbox (data connected with settings) and datagridview in my form. I am trying to fill 0th column in datagridview from listbox. Can not find the solution on web, so I've tried this one :

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

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

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

        Next

And the result is just one item in column 0 : "System.Windows.Forms.ListBox+ObjectCollection"

结果只是第 0 列中的一项:“System.Windows.Forms.ListBox+ObjectCollection”

Obviously that has something to do with setting (where data from listbox is saved) and I don't know how to fix it.

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

P.S. That code above also deletes items from listbox!?

PS 上面的代码也会从列表框中删除项目!?

EDIT :

编辑 :

    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

and

    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

回答by King of kings

Try this code. it works

试试这个代码。有用

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

回答by GoroundoVipa

This Will Do The Trick...

这将起作用......

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

回答by King of kings

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

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

    Next