[英]Add DataGridViewComboBoxColumn to DataGridView, with different values in each row
Found many articles online, but to no avail for the solution I am looking for. 在网上找到了许多文章,但对我寻找的解决方案无济于事。
My task: Add DataGridViewComboBoxColumn to datagridview, but each row should have different values in it's respective combobox. 我的任务:将DataGridViewComboBoxColumn添加到datagridview,但是每一行在其相应的组合框中均应具有不同的值。
This is my code, 这是我的代码
dgv.Columns.Clear();
dgv.Rows.Clear();
dgv.Columns.Add("package", "Version");
foreach (DirectoryInfo item in (new DirectoryInfo("E:\\MyProjects")).GetDirectories())
{
dgv.Rows.Add(item.Name);
}
DataGridViewButtonColumn downloadColumn = new DataGridViewButtonColumn
{
HeaderText = "Download",
Name = "download",
Text = "Download",
UseColumnTextForButtonValue = true
};
dgv.Columns.Add(downloadColumn);
// Add Combobox column
DataGridViewComboBoxColumn books = new DataGridViewComboBoxColumn();
books.HeaderText = "Books";
books.Items.Add("Book1");
books.Items.Add("Book2");
dgv.Columns.Insert(1, books);
The drop downs are just empty. 下拉菜单只是空的。 I have tried putting books.Items.Add(sub directories.Name) in a foreach loop for each row, then the combobox will not get any values at all. 我已经尝试将books.Items.Add(子目录。名称)放在foreach循环的每一行中,然后组合框根本不会获得任何值。 I am trying to show sub directories in the combo box. 我正在尝试在组合框中显示子目录。 Not sure why it's not displaying the values (books.Items is still having the items I added, they are just not displaying). 不知道为什么它不显示值(books.Items仍然有我添加的项目,它们只是不显示)。
What's driving me crazy is the same piece of code works on my home computer (Windows 10, Visual Studio 2013, .NET 4.5), but not on my office computer (Windows 7, Visual Studio 2012, .NET 4.5). 使我发疯的是,同一段代码可在我的家用计算机(Windows 10,Visual Studio 2013,.NET 4.5)上工作,而在我的办公计算机(Windows 7,Visual Studio 2012,.NET 4.5)上却无法工作。 Can someone please help! 有人可以帮忙吗?
I am getting this exception - DataGridView:System.ArgumentException: DataGridViewComboBoxCell value is not valid.To replace this default dialog please handle the DataError event. 我收到此异常-DataGridView:System.ArgumentException:DataGridViewComboBoxCell值无效。要替换此默认对话框,请处理DataError事件。
Many thanks in advance!!!! 提前谢谢了!!!!
Fixed! 固定! So I set the data grid view to read-only. 因此,我将数据网格视图设置为只读。 After making readonly = false, all works as expected!! 设置readonly = false后,所有操作均按预期进行!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.