问题 如何将comboBoxcolumn添加到dataTable而不仅仅是网格?我尝试将“连接器名称”列设置为组合框,但显然我做得不对。
我拥有的内容(摘录):
if (_DtMain.Columns.Count == 0)
{
var cols = new List<DataColumn>();
cols.Add(new DataColumn("PocketName", typeof(string)));
cols.Add(new DataColumn("KeyIndex", typeof(string)));
cols.Add(new DataColumn("SortOrder", typeof(int)));
cols.Add(new DataColumn("ConnectorName", typeof(DataGridViewComboBoxColumn )));
_DtMain.Columns.AddRange(cols.ToArray());
dataGridView1.DataSource = _DtMain.DefaultView;
}
修改
这是我的新解决方案:
if (_DtMain.Columns.Count == 0)
{
var colPN = new DataGridViewTextBoxColumn();
var colCN = new DataGridViewComboBoxColumn();
var colKI = new DataGridViewTextBoxColumn();
var colSO = new DataGridViewTextBoxColumn();
dgvHeaderEdit.Columns.Add(colPN);
dgvHeaderEdit.Columns.Add(colCN);
dgvHeaderEdit.Columns.Add(colKI);
dgvHeaderEdit.Columns.Add(colSO);
colPN.HeaderText = "Pocket Name";
colPN.DataPropertyName = "Pocket Name";
colCN.HeaderText = "Connector Name";
colCN.DataPropertyName = "Connector Name";
colKI.HeaderText = "Key Index";
colKI.DataPropertyName = "Key Index";
colSO.HeaderText = "Sort Order";
colSO.DataPropertyName = "Sort Order";
_DtMain.Columns.Add(new DataColumn("Pocket Name", typeof(string)));
_DtMain.Columns.Add(new DataColumn("Connector Name", typeof(string)));
_DtMain.Columns.Add(new DataColumn("Key Index", typeof(string)));
_DtMain.Columns.Add(new DataColumn("Sort Order", typeof(int)));
dgvHeaderEdit.AutoGenerateColumns = false;
dgvHeaderEdit.DataSource = _DtMain.DefaultView;
答案 0 :(得分:0)
以这种方式使用它: 为Grid的DataSource构建dataTable时:
DataTable dt = new DataTable();
dt.Columns.Add("PocketName", typeof(string));
dt.Columns.Add("KeyIndex", typeof(string));
dt.Columns.Add("SortOrder", typeof(int));
dt.Columns.Add("ConnectorName", typeof(int));
dgv.DataSource = dt;
现在为GridView创建一个ComboBox列&amp;这个ComboBox的数据源。 添加所有值以在dtConnectorSource中的ComboBox中显示 使用ComboBox中所需的值填充dtConnectorSource。
DataTable dtConnectorSource = new DataTable();
dtConnectorSource.Columns.Add("ConnectorName", typeof(int));
dtConnectorSource.Columns.Add("ConnectorNameDisplay", typeof(String));
DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn ();
cmb.DataSource = dtConnectorSource;
cmb.DisplayMember = "ConnectorNameDisplay";
cmb.ValueMember = "ConnectorName";
cmb.DataPropertyName = "ConnectorName";
dgv.Columns.Add(cmb);