Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我可以使用DataGridViewComboxColumn显示类对象列表吗?_C#_.net_Datagridviewcombobox - Fatal编程技术网

C# 我可以使用DataGridViewComboxColumn显示类对象列表吗?

C# 我可以使用DataGridViewComboxColumn显示类对象列表吗?,c#,.net,datagridviewcombobox,C#,.net,Datagridviewcombobox,我试图用类对象列表填充datagridviewcomboxcolumn(重载ToString)。对于常规组合框,这种方法非常适合我。但是,一旦我将DataGridViewComboBoxColumn加载到dataGridView中,每次单击datagrid时都会抛出一个错误 这里是错误 The Following Exception Occurred in the DataGridView: System.ArgumentException: DataGridViewComboBoxCell v

我试图用类对象列表填充
datagridviewcomboxcolumn
(重载
ToString
)。对于常规组合框,这种方法非常适合我。但是,一旦我将
DataGridViewComboBoxColumn
加载到
dataGridView
中,每次单击datagrid时都会抛出一个错误

这里是错误

The Following Exception Occurred in the DataGridView:
System.ArgumentException: DataGridViewComboBoxCell value is not valid.
To replace this default dialog please handle the DataError event.
这是在错误对话框之前执行的代码:

DataTable dt = new DataTable();

List<mclass> MObjects = new List<mclass>();

DataGridViewComboBoxColumn objcolumn = new DataGridViewComboBoxColumn();

objcolumn.DataSource = MObjects;

objcolumn.ValueType = typeof(mclass);

dataGridView1.DataSource = dt;

dataGridView1.Columns.Add(objcolumn);
但是,这也造成了同样的例外,时间更长(我省略了大部分内容,可根据要求提供):


我相信您的
ValueType
必须是
string

typeof
部分应该是提取值的方法,可能在您的
ToString()

中,您的问题可能是由ComboBoxStyle下拉列表引起的,这意味着文本是可编辑的。尝试将样式更改为DropDownList,这意味着该文本不可编辑。该文本当前不可编辑。我相信这是你默认的建议。好奇者和好奇者。显然,对于任何数据类型,甚至字符串,这都是ComboBoxColumn的常见问题。我尝试过重载datagridview的错误处理,但没有结果,下面是代码:private void DataGridView1_DataError(对象发送者,DataGridViewDataErrorEventArgs anError){anError.Cancel=true;}这仍然不起作用,尽管这在我脑海中正确地消除了错误。有什么想法吗?你能详细说明一下吗?我是否应该创建一个在mclass类中返回字符串的方法?然后让type=that方法?
dt.Columns.Add("OBJList", typeof(mclass));
The following exception occurred in the DataGridView:
System.FormatException: Invalid cast from 'System.String' to 'Namespace.mobject'. --->      
    System.InvalidCastException: Invalid cast from 'System.String' to 'Namespace.mobject'.