如何使用此代码将combobox添加到datagridview? VB.NET

时间:2015-03-09 08:09:22

标签: datagridview vb.net-2010

这是我在数据网格视图中显示数据的代码,它显示的一些数据只需要是/否答案,所以我想添加一个组合框以限制选择:

    Dim con As SqlConnection
    Dim adap As SqlDataAdapter
    Dim ds As DataSet
    Dim cmdbl As SqlCommandBuilder


  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Call populateCombo()
        Try
            con = New SqlConnection
            con.ConnectionString = "Data Source=mssql;Initial Catalog=VRC_Royalty;User ID=sa;Password="
            con.Open()

            adap = New SqlDataAdapter("SELECT * FROM tblsongmasterfile ORDER BY Primaryid ASC", con)
            ds = New System.Data.DataSet()
            adap.Fill(ds, "Song_list")
            DataGridView1.DataSource = ds.Tables(0)


        Catch ex As Exception
            MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

1 个答案:

答案 0 :(得分:0)

要添加GridViewComboBoxColumn,您需要执行以下操作:

Dim cmb As New DataGridViewComboBoxColumn()
cmb.HeaderText = "Yes or No"
cmb.Name = "cmb"
cmb.MaxDropDownItems = 2
cmb.Items.Add("Yes")
cmb.Items.Add("No")
DataGridView1.Columns.Add(cmb)

如果在您的查询中检索到此列的数据,您可能需要查看禁用AutoGenerateColumns并按所需顺序手动添加每列。