创建数据网格视图列并将其与查询结果绑定.将其添加到 DataGridView

本文关键字:结果 查询 绑定 添加 DataGridView 数据 视图 网格 创建 数据网 | 更新日期: 2023-09-27 17:57:02

我拼命地尝试以编程方式将 DataGridViewComboBoxColumn 添加到我的 DataGridView 中。这是我到目前为止的代码。

DataTable dt = new DataTable();
dt = _userBL.getUsersTable().DefaultView.ToTable(false, "Person_ID", "FirstName", "LastName", "Title", "Username");
dataGridView1.DataSource = dt;
DataGridViewComboBoxColumn ComboBoxCell = new DataGridViewComboBoxColumn();
ComboBoxCell.Name = "State";
ComboBoxCell.ValueMember = "State";//ComboBoxCell.DisplayMember = "State";
ComboBoxCell.DisplayMember = "State"; 
ComboBoxCell.DataSource = _userBL.getUsersTable().DefaultView.ToTable(false, "State");
this.dataGridView1.Columns.Add(ComboBoxCell);

但是我在组合框列中没有得到任何数据,并且由于我添加了数据源,因此无法在其中添加项目(活动,非活动)。此外,当我单击它时,即使对于空组合框,也应该会出现一个小下拉菜单,但它没有。提前感谢!

创建数据网格视图列并将其与查询结果绑定.将其添加到 DataGridView

试试这个。

DataGridViewComboBoxColumn combo = (DataGridViewComboBoxColumn)dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[1].OwningColumn;
            dt.Clear();
            da = new SqlDataAdapter("select name from tblData", con);
            da.Fill(dt);
            combo.DataSource = dt;
            combo.DisplayMember = "name";