C# 如何用不同的数据填充每个 DataGridViewComboBoxCell?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/446600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to populate each DataGridViewComboBoxCell with different data?
提问by
i have two DataGridViewComboBoxColumn that i add at run time i need the items of the first DataGridViewComboBoxColumn to stay the same in all the rows of the gridview but i want the items of the second DataGridViewComboBoxColumn to be different from row to the other depending on the selected item of the first DataGridViewComboBoxColumn
我有两个 DataGridViewComboBoxColumn,我在运行时添加我需要第一个 DataGridViewComboBoxColumn 的项目在 gridview 的所有行中保持相同,但我希望第二个 DataGridViewComboBoxColumn 的项目从行到另一个不同,具体取决于所选第一个 DataGridViewComboBoxColumn 的项目
if we say the first DataGridViewComboBoxColumn represents the locations and the second DataGridViewComboBoxColumn to represent the sublocations. so i want the second DataGridViewComboBoxColumn items to be the sublocations of the selected location from the first DataGridViewComboBoxColumn
如果我们说第一个 DataGridViewComboBoxColumn 代表位置,第二个 DataGridViewComboBoxColumn 代表子位置。所以我希望第二个 DataGridViewComboBoxColumn 项目是第一个 DataGridViewComboBoxColumn 中所选位置的子位置
回答by Sergiu Damian
One idea would be to use a secondary Binding Source for the "SubLocations" column. This BindingSource can be filtered by the LocationId selected in the "Locations" column. The key to do this is to use the EditingControlShowing and CellValueChanged events of the grid to set the proper filtering on the SubLocations column when the selected Location changes.
一种想法是对“SubLocations”列使用辅助绑定源。此 BindingSource 可以通过在“位置”列中选择的 LocationId 进行过滤。这样做的关键是使用网格的 EditingControlShowing 和 CellValueChanged 事件在选定的位置更改时在 SubLocations 列上设置适当的过滤。
There is one example here.
有一个例子在这里。
回答by Joe Ratzer
Check this out, I think it outlines what you need:
看看这个,我认为它概述了你需要的东西:
http://www.timvw.be/2007/01/17/exploring-datagridviewcomboboxcolumn-databinding/
http://www.timvw.be/2007/01/17/exploring-datagridviewcomboboxcolumn-databinding/
回答by Sorin Comanescu
One option is to change the datasource at cell level for sublocations.
一种选择是在单元级别更改子位置的数据源。
Supposing the grid is named grid
and the two grid columns were named locationsColumn
respectively subLocationsColumn
:
假设网格被命名grid
并且两个网格列分别被命名locationsColumn
为subLocationsColumn
:
private void Form1_Load(object sender, EventArgs e)
{
locationsColumn.DataSource = new string[] { "Location A", "Location B" };
}
then, on grid's CellEndEdit
event:
然后,关于网格的CellEndEdit
事件:
private void grid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if(locationsColumn.Index == e.ColumnIndex)
{
DataGridViewComboBoxCell subLocationCell =
(DataGridViewComboBoxCell)(grid.Rows[e.RowIndex].Cells["subLocationsColumn"]);
string location = grid[e.ColumnIndex, e.RowIndex].Value as String;
switch (location)
{
case "Location A":
subLocationCell.DataSource = new string[] {
"A sublocation 1",
"A sublocation 2",
"A sublocation 3"
};
break;
case "Location B":
subLocationCell.DataSource = new string[] {
"B sublocation 1",
"B sublocation 2",
"B sublocation 3"
};
break;
default:
subLocationCell.DataSource = null;
return;
}
}
}
Some additional handling is necessary when the location changes for existing rows but this is the basic idea.
当现有行的位置发生变化时,需要进行一些额外的处理,但这是基本思想。