我在内部使用了combobox的Datagridview,我无法在代码中设置索引 我看过this和this - 都不起作用。
这是我的代码:
dgShuffle.DataSource = dtCards;
DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
cmb.HeaderText = "Select Data";
cmb.Name = "cmb";
cmb.MaxDropDownItems = 2;
cmb.Items.Add("real");
cmb.Items.Add("sham");
dgShuffle.Columns.Add(cmb);
for (int i = 0; i < dgShuffle.RowCount; i++)
{
(dgShuffle.Rows[i].Cells[6] as DataGridViewComboBoxCell).Value = "real";
// dgShuffle.Rows[i].Cells[6].Value = "real";
}
按代码设置我的意思是:以编程方式(取决于datatble中的值)。 我根本不会得到错误。该值根本不会显示在组合框中
我检查了组合框的索引,这是正确的,低于我的即时窗口的输出:
?dgShuffle.Rows [I] .Cells [6]
{DataGridViewComboBoxCell {ColumnIndex = 6,RowIndex = 0}} [System.Windows.Forms.DataGridViewComboBoxCell]:{DataGridViewComboBoxCell {ColumnIndex = 6,RowIndex = 0}} base:{DataGridViewComboBoxCell {ColumnIndex = 6,RowIndex = 0}} AccessibilityObject:{System.Windows.Forms.DataGridViewCell.DataGridViewCellAccessibleObject} ColumnIndex:6 ContentBounds:{X = 0 Y = 12 Width = 0 Height = 0} ContextMenuStrip:null DefaultNewRowValue:null 显示:false EditedFormattedValue:“” EditType:{Name =“DataGridViewComboBoxEditingControl”FullName =“System.Windows.Forms.DataGridViewComboBoxEditingControl”} ErrorIconBounds:{X = 0 Y = 0 Width = 0 Height = 0} ErrorText:“” FormattedValue:“” FormattedValueType:{Name =“String”FullName =“System.String”} 冻结:假 HasStyle:false InheritedState:可调整大小| ResizableSet |可见 InheritedStyle:{DataGridViewCellStyle {BackColor = Color [Window],ForeColor = Color [ControlText],SelectionBackColor = Color [Highlight],SelectionForeColor = Color [HighlightText],Font = [Font:Name = Microsoft Sans Serif,Size = 7.8,Units = 3,GdiCharSet = 177,GdiVerticalFont = False],WrapMode = False,Alignment = MiddleLeft}} IsInEditMode:false OwningColumn:{DataGridViewComboBoxColumn {Name = cmb,Index = 6}} OwningRow:{DataGridViewRow {Index = 0}} PreferredSize:{Width = 43 Height = 26} ReadOnly:false 可调整大小:真实 RowIndex:0 选中:假 尺寸:{宽度= 100高度= 24} 风格:{DataGridViewCellStyle {}} 标签:null ToolTipText:“” 值:null ValueType:{Name =“Object”FullName =“System.Object”} 可见:真实
将此代码复制到Form1_Load:
DataTable dtCards;
dtCards = new DataTable();
dtCards.Columns.Add("printedString");
dtCards.Rows.Add("1");
dtCards.Rows.Add("2");
dtCards.Rows.Add("3");
dtCards.Rows.Add("4");
dataGridView1.DataSource = dtCards;
DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
cmb.HeaderText = "Select Data";
cmb.Name = "cmb";
cmb.MaxDropDownItems = 2;
cmb.Items.Add("real");
cmb.Items.Add("sham");
dataGridView1.Columns.Add(cmb);
for (int i = 0; i < dataGridView1.RowCount; i++)
{
(dataGridView1.Rows[i].Cells[6] as DataGridViewComboBoxCell).Value = "real";
// dgShuffle.Rows[i].Cells[6].Value = "real";
}
我缺少什么?
答案 0 :(得分:1)
您正在使用与数据源绑定的datagridview。您需要在数据源中指定该值。
在数据源中添加DataGridViewComboBoxColumn
的值
然后将DataGridViewComboBoxColumn.DataPropertyName
设置为数据源列/属性的名称
DataTable dtCards;
dtCards = new DataTable();
dtCards.Columns.Add("printedString");
dtCards.Columns.Add("comboboxValue", typeof(String)); //adding column for combobox
dtCards.Rows.Add("1", "real");
dtCards.Rows.Add("2", "real");
dtCards.Rows.Add("3", "real");
dtCards.Rows.Add("4", "real");
DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
cmb.HeaderText = "Select Data";
cmb.Name = "cmb";
cmb.MaxDropDownItems = 2;
cmb.Items.Add("real");
cmb.Items.Add("sham");
cmb.DataPropertyName = "comboboxValue"; //Bound value to the datasource
dataGridView1.Columns.Add(cmb);
dataGridView1.DataSource = dtCards;