在datagridview内的组合框中设置值 [英] set value in combobox inside datagridview
问题描述
我有带组合框的Datagridview,我无法在代码
中设置索引,我读了此和此 -两者都不起作用。
I Have Datagridview with combobox inside and i can't set the index in code I read this and this - both not work.
这是我的代码:
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中的值) 。
我根本不出错。该值根本不会显示在组合框中
To set by code i mean : programmatically (depend on the value in datatble). I Dont Get error at all. the value simply not display in the combobox
我检查了组合框的索引,这是正确的,在我即时窗口的输出下方:
I checked the index of the combobox and this is correct , below the output from my immediate window:
?dgShuffle.Rows [i] .Cells [6]
?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}}
ColumnIndex:6
ContentBounds:{X = 0 Y = 12宽度= 0高度= 0}
ContextMenuStrip:空
DefaultNewRowValue :空
显示:否
EditedFormattedValue:
EditType:{Name = DataGridViewComboBoxEditingControl FullName = System.Windows.Forms.DataGridViewComboBoxEditingControl}
ErrorIconBounds:{X = 0 Y = 0智慧h = 0高度= 0}
ErrorText:
FormattedValue:
FormattedValueType:{Name = String FullName = System.String}
冻结:false
HasStyle:否
InheritedState:可调整大小| ResizableSet |可见的
InheritedStyle:{DataGridViewCellStyle {BackColor = Color [Window],ForeColor = Color [ControlText],SelectionBackColor = Color [Highlight],SelectionForeColor = Color [HighlightText],Font = [字体:名称= Microsoft Sans Serif,大小= 7.8,单位= 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
可调整大小:true
RowIndex:0
选中:false
大小:{Width = 100 Height = 24}
样式:{DataGridViewCellStyle {}}
标签:null
ToolTipText:
值:空
值类型:{Name = Object FullName = System.Object}
可见:true
{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 Displayed: 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"} Frozen: false HasStyle: false InheritedState: Resizable | ResizableSet | Visible 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 Resizable: true RowIndex: 0 Selected: false Size: {Width = 100 Height = 24} Style: {DataGridViewCellStyle { }} Tag: null ToolTipText: "" Value: null ValueType: {Name = "Object" FullName = "System.Object"} Visible: true
下一次尝试:
Next try :
- 我创建了一个新的Winfo rms项目
- 将datagridview拖动到默认表单(从工具箱中)
-
将此代码复制到Form1_Load:
- I create a new winforms project
- Drag datagridview to the default form (from the toolbox)
Copy this code to 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";
}
我想念的是什么
推荐答案
您正在使用绑定到数据源的datagridview。您需要在数据源中指定该值。
You are using datagridview bounded to the data source. You need specify that value in the datasource.
为数据源中的 DataGridViewComboBoxColumn
添加值
然后将 DataGridViewComboBoxColumn.DataPropertyName
设置为数据源的列/属性的名称
Add value for DataGridViewComboBoxColumn
in the data source
Then set DataGridViewComboBoxColumn.DataPropertyName
to the name of the column/property of your data source
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;
这篇关于在datagridview内的组合框中设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!