这是我的代码:
SqlDataAdapter GridDataAdapter = new SqlDataAdapter(query, con);
DataSet GridDataSet = new DataSet();
GridDataAdapter.Fill(GridDataSet, tbln);
dataGridView1.DataSource = GridDataSet;
DataGridViewComboBoxColumn dgvCB = new DataGridViewComboBoxColumn();
dataGridView1.DataMember = tbln;
在这里,我想从数据库表列中获取DataGridViewComboBox的数据。
如何在不使用DataReader的情况下填充DataGridViewComboBoxColumn?
答案 0 :(得分:0)
您已经填充了数据集,如果它包含您希望DGVCombo包含的值,则可以遍历行集合。因此,最直接的方式是:
foreach(DataRow r in GridDataSet.Tables[0].Rows)
{
dgvCB.Items.Add(r["MyColumn"]);
}
其中.Tables [0]有你想要的专栏(" MyColumn")......
答案 1 :(得分:0)
这应该是你需要的......
DataGridViewComboBoxColumn dgvCB = new DataGridViewComboBoxColumn();
dgvCB.Name = "lastname";
dgvCB.DataSource = tbln;
dgvCB.HeaderText = "Last";
//uncomment this to actually select the value in the combo box
//dgvCB.DataPropertyName = "lastname";
dgvCB.ValueMember = "lastname";
dataGridView1.Columns.Add(dgvCB);