问题描述
我对winform应用程序不熟悉。
我有一个数据网格视图,我在其中添加了一个组合框列。
我想为每一行设置一个组合框的值。
我正在做的事情:
Hi,
I am not much familiar with winform applications.
I have a datagrid view in which I am adding a combobox column.
I want to set the value of a combobox for each row.
This is what I am doing:
private void BindGrid(DataTable Table)
{
dgvSchemaMapped.AutoGenerateColumns = false;
dgvSchemaMapped.Columns[0].Name = "COLUMN_NAME";
dgvSchemaMapped.Columns[0].HeaderText = "Column";
dgvSchemaMapped.Columns[0].DataPropertyName = "COLUMN_NAME";
dgvSchemaMapped.Columns[1].Name = "ALIAS_COLUMN";
dgvSchemaMapped.Columns[1].HeaderText = "Alias Column";
dgvSchemaMapped.Columns[1].DataPropertyName = "ALIAS_COLUMN";
dgvSchemaMapped.Columns[2].Name = "TYPE_NAME";
dgvSchemaMapped.Columns[2].HeaderText = "Data Type";
dgvSchemaMapped.Columns[2].DataPropertyName = "TYPE_NAME";
dgvSchemaMapped.DataSource = Table;
//Add combobox column to gird
DataGridViewComboBoxColumn AliasDataTypeCombo = new DataGridViewComboBoxColumn();
FillDataTypeCombo(AliasDataTypeCombo);
dgvSchemaMapped.Columns.Add(AliasDataTypeCombo);
// Set default value to aliasDataType combobox
SetDefaultDataType();
}
private void SetDefaultDataType()
{
for (int i = 0; i < dgvSchemaMapped.Rows.Count - 1; i++)
{
string dataType = dgvSchemaMapped.Rows[i].Cells["TYPE_NAME"].Value.ToString();
switch (dataType.ToLower())
{
case "int":
case "tinyint":
case "smallint":
(dgvSchemaMapped.Rows[i].Cells[3]).Value = ((KeyValuePair<string, int>)(((dgvSchemaMapped.Rows[i].Cells[3] as DataGridViewComboBoxCell).Items[3]))).Value;
break;
case "char":
case "nchar":
case "varchar":
case "nvarchar":
(dgvSchemaMapped.Rows[i].Cells[3]).Value = ((KeyValuePair<string, int>)((dgvSchemaMapped.Rows[i].Cells[3] as DataGridViewComboBoxCell).Items[18])).Value;
break;
case "real":
case "decimal":
(dgvSchemaMapped.Rows[i].Cells[3]).Value = ((KeyValuePair<string, int>)((dgvSchemaMapped.Rows[i].Cells[3] as DataGridViewComboBoxCell).Items[12])).Value;
break;
case "datetime":
(dgvSchemaMapped.Rows[i].Cells[3]).Value = ((KeyValuePair<string, int>)((dgvSchemaMapped.Rows[i].Cells[3] as DataGridViewComboBoxCell).Items[22])).Value;
break;
}
}
}
private void FillDataTypeCombo(DataGridViewComboBoxColumn AliasDataTypeCombo)
{
Dictionary<string, int> lstDataTypes = new Dictionary<string, int>();
GetBuilInDataTypes(lstDataTypes);
AliasDataTypeCombo.DataSource = new BindingSource(lstDataTypes, null);
AliasDataTypeCombo.ValueMember = "value";
AliasDataTypeCombo.DisplayMember = "key";
}
当绑定网格时,组合框中会有值。但我无法为每个组合框设置默认值。
我做错了什么?
任何帮助表示感谢。
谢谢,
乐。
我的尝试:
我尝试将网格单元格转换为组合单元格并为其赋值。
但这没有给我任何结果。
The comboboxes do have values in them when grid is bound. But I am not able to set the default value to each combobox.
What am I doing wrong?
Any help appreciated.
Thanks,
Lok.
What I have tried:
I tried casting the grid cell to combobox cell and assign value to it.
But this gives me no result.
推荐答案
AliasDataTypeCombo.Items.AddRange("One", "Two", "Three", "Four");
也可能是范围问题您直接分配 BindingSource
,尝试使用公共BindingSource变量。
Could also be a scope problem as you assign the BindingSource
directly, try with a public BindingSource variable.
for (int i = 0; i < dgvSchemaMapped.Rows.Count - 1; i++)
2)您使用的是DataGridView.DataError事件吗?在调试期间这几乎是必不可少的,因为DGV具有吞咽异常的恶习,并且事件通常是揭示数据问题的唯一方式。例如
2) Are you using the DataGridView.DataError event. It is almost essential during debugging as the DGV has a nasty habit of swallowing exceptions and the event is often the only way to reveal a problem with the data. For example
comboboxCell.Value = SomethingNotInTheItemsList;
是一个数据错误,会给出一个空白单元格。
3)在switch中,dataType.ToLower()是一个有效值。我问是否有任何开关部分(案例)执行。可能会添加一个带有异常throw语句的默认情况,以突出显示dataType中的意外值。
关于代码的两条评论
复杂的索引/强制转换/索引/强制转换/分配一个班轮真的很难理解,并将其拆分成单独的部分将有助于调试。只有最后两行需要在开关内部。
is a data error and will give a blank cell.
3) In the switch is dataType.ToLower() a valid value. I'm asking if any of the switch sections (cases) execute. Possibly add a default case with an exception throw statement to highlight an unexpected values in dataType.
Two comments about the code
That complex index/cast/index/cast/assign one liner is really difficult to understand and splitting it up into individual parts will aid debugging. Only the last two lines need to be inside the switch.
DataGridViewRow row = grid.Rows[i];
DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)row.Cells[3];
DataGridViewComboBoxCell.ObjectCollection items = cell.Items;
KVP defaultItem = (KVP)items[18]; // KVP is an alias for the full KeyValuePair<>
cell.Value = defaultItem.Value;
为什么作为BindingSource的组合框项目的字典必须隐式转换为IBindingList。
Alan。
Why a Dictionary for the combobox items as the BindingSource has to do an implicit conversion to an IBindingList.
Alan.
这篇关于将值分配给datagridview中的组合框列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!