使用c#填充Windows应用程序中DataGridView内部的ComboBox

时间:2016-05-08 02:31:23

标签: c# datagridview combobox

我正在尝试从数据库中填充DGV中存在的组合框,但它无法正常工作。我的代码是: -

    private void popParent()
    {
        DataSet ds = new DataSet();
        string sqlPS = @"SELECT * FROM tbl_acc_ledgergroup_msb";
        try
        {
            using (FbConnection conPS = new FbConnection(connectionString))
            {
                conPS.Open();
                using (FbCommand cmdPS = new FbCommand(sqlPS, conPS))
                {
                    using (FbDataAdapter da = new FbDataAdapter())
                    {
                        da.SelectCommand = cmdPS;
                        da.Fill(ds);
                        DataGridViewComboBoxColumn cell0=(DataGridViewComboBoxColumn)(dataGridView1.Columns["Fee Head"]);
                        cell0.DataSource = ds.Tables[0].Columns[0].Table.DefaultView;
                        cell0.ValueMember = "lg_id";
                        cell0.DisplayMember = "lg_name";
                    }
                }
            }
        }
        catch (FbException ex)
        {
            MessageBox.Show("PP-->>" + ex.Message);
        }
    }

目前程序退出时没有从行

给出任何错误消息
cell0.DataSource = ds.Tables[0].Columns[0].Table.DefaultView;

请告知我的代码有什么问题。提前谢谢。

1 个答案:

答案 0 :(得分:0)

改变:

cell0.DataSource = ds.Tables[0].Columns[0].Table.DefaultView;

cell0.DataSource = ds.Tables[0];