【发布时间】:2015-10-05 01:01:28
【问题描述】:
我有内部带有组合框的 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";
}
通过代码设置我的意思是:以编程方式(取决于数据表中的值)。 我根本不会出错。该值根本不会显示在组合框中
我检查了组合框的索引,这是正确的,在我的即时窗口的输出下方:
?dgShuffle.Rows[i].Cells[6]
{DataGridViewComboBoxCell { ColumnIndex=6, RowIndex=0 }} [System.Windows.Forms.DataGridViewComboBoxCell]: {DataGridViewComboBoxCell { ColumnIndex=6, RowIndex=0 }} 基数:{DataGridViewComboBoxCell { ColumnIndex=6, RowIndex=0 }} AccessibilityObject:{System.Windows.Forms.DataGridViewCell.DataGridViewCellAccessibleObject} 列索引:6 内容边界:{X = 0 Y = 12 宽度 = 0 高度 = 0} 上下文菜单条:空 默认新行值:空 显示:假 编辑格式化值:“” EditType: {Name = "DataGridViewComboBoxEditingControl" FullName = "System.Windows.Forms.DataGridViewComboBoxEditingControl"} ErrorIconBounds: {X = 0 Y = 0 宽度 = 0 高度 = 0} 错误文本:“” 格式化值:“” FormattedValueType: {Name = "String" FullName = "System.String"} 冻结:假 有样式:假 InheritedState:可调整大小 |可调整大小集 |可见的 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:假 OwningColumn: {DataGridViewComboBoxColumn { Name=cmb, Index=6 }} OwningRow: {DataGridViewRow { 索引=0 }} PreferredSize: {宽度 = 43 高度 = 26} 只读:假 可调整大小:真 行索引:0 选择:假 尺寸:{宽度 = 100 高度 = 24} 样式:{DataGridViewCellStyle { }} 标签:空 工具提示文本:“” 值:空 值类型:{Name = "Object" FullName = "System.Object"} 可见:真
下一次尝试:
- 我创建了一个新的 winforms 项目
- 将 datagridview 拖到默认表单(从工具箱中)
-
将此代码复制到 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"; }
我错过了什么???
【问题讨论】:
-
“我无法在代码中设置索引”是什么意思?你的目标到底是什么?
-
用
try/catch包裹它,你会收到错误吗?我不明白需要强制转换,代码dgShuffle.Rows[i].Cells[6].Value = "real";应该可以正常工作,只要该值在下拉列表中可用。 -
不太明白你的问题。我刚刚测试了你的代码块,它工作正常。您是否设置了错误的列索引?
-
我编辑了我的问题。
-
您的 ComboBoxColumn 显示,您可以在运行时更改其值但不能以编程方式? Ps:仍然没有在您的代码中看到任何错误。您能否提供影响您的 DataGridView 的其他代码?也许尝试删除您的 DataGridView 并创建一个新的以避免设计时错误?
标签: c# winforms datagridview combobox