Datagridview combobox每一行的列值不同 [英] Datagridview comboboxcolumn different values for each row
问题描述
我想使用c#创建带有comboboxcolumns的datagridview。
I want to create a datagridview with comboboxcolumns using c#.
问题是我不知道如何为每一行的组合框指定不同的值。
The problem is that I dont know how to give different values for combobox in each row.
DataTable dt = new DataTable();
dt.Columns.Add("state");
dt.Columns.Add("city");
dt.Rows.Add("a1", "b1");
dt.Rows.Add("a1", "b2");
dt.Rows.Add("a2", "b3");
dt.Rows.Add("a2", "b4");
dt.Rows.Add("a3", "b5");
DataGridViewComboBoxColumn comboStates = new DataGridViewComboBoxColumn();
comboStates.HeaderText = "HeaderText_1";
this.dataGridView1.Columns.Insert(0, comboStates);
DataGridViewComboBoxColumn comboCities = new DataGridViewComboBoxColumn();
comboCities.HeaderText = "HeaderText_2";
this.dataGridView1.Columns.Insert(1, comboCities);
for (int i = 0; i < dt.Rows.Count; i++)
{
dataGridView1.Rows.Add();
comboStates.Items.Add(dt.Rows[i][0]);
DataGridViewComboBoxCell stateCell = (DataGridViewComboBoxCell) (dataGridView1.Rows[i].Cells[0]);
stateCell.Value = comboStates.Items[i];
comboCities.Items.Add(dt.Rows[i][1]);
DataGridViewComboBoxCell cityCell = (DataGridViewComboBoxCell)(dataGridView1.Rows[i].Cells[1]);
cityCell.Value = comboCities.Items[i];
}
此示例给出以下结果:
每行:
comboboxcolumnstate:
comboboxcolumnstate :
a1
a1
a2
a2
a3
comboboxcolumncity:
comboboxcolumncity :
b1
b2
b3
b4
b5
我知道这是正常现象,因为我正在遍历数据表。
I know this is normal because I am looping through datatable.
那么我怎么得到这个结果:
So how can i get this result:
行1:comboboxcolumnstate comboboxcolumncity
row1: comboboxcolumnstate comboboxcolumncity
a1 b1 - b2
row2:comboboxcolumnstate comboboxcolumncity
row2: comboboxcolumnstate comboboxcolumncity
a2 b3 - b4
row2 :comboboxcolumnstate comboboxcolumncity
row2: comboboxcolumnstate comboboxcolumncity
a3 b5
我是C#的新手。我进行了很多搜索,但没有找到解决此问题的解决方案。
谢谢
I am new to C#. I searched a lot but I didn't find a solution which solve this issue. Thanks
推荐答案
下面是一个示例代码,可以帮助您入门。
Here is an example code that should get you started.
首先,我为每个州创建一个值(城市)列表。为此, Dictionary
集合很方便:
First I create a list of values (cities) for each state. For this the Dictionary
collection comes handy:
Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
for (int i = 0; i < dt.Rows.Count; i++)
{
string state = dt.Rows[i][0].ToString();
string city = dt.Rows[i][1].ToString();
if (! dict.Keys.Contains(state )) dict.Add(state, new List<string>());
dict[state].Add(city);
}
我遍历表,并为每一行添加一个新条目
I loop over your table and for each row I add a new entry for the city and, if necessary, for the state.
现在,我们修改DGV的设置,以使用这些列表
在每个 DataGridViewComboBoxCell
的项目
中。
Now we modify the setup of the DGV to use those Lists
in the Items
of each of the DataGridViewComboBoxCell
.
注意:此处的关键是使用 DataGridViewComboBoxCells
,不使用 DataGridViewComboBoxColumns
!
Note: Here the key is to use the DataGridViewComboBoxCells
, and not the DataGridViewComboBoxColumns
!
为了便于访问,我再次为州和城市值创建了字符串变量。
For easier access I create string variables for the state and city values again..
for (int i = 0; i < dt.Rows.Count; i++)
{
string state = dt.Rows[i][0].ToString();
string city = dt.Rows[i][1].ToString();
dataGridView1.Rows.Add();
DataGridViewComboBoxCell stateCell =
(DataGridViewComboBoxCell)(dataGridView1.Rows[i].Cells[0]);
stateCell.Items.AddRange(dict.Keys.ToArray());
stateCell.Value = state;
DataGridViewComboBoxCell cityCell =
(DataGridViewComboBoxCell)(dataGridView1.Rows[i].Cells[1]);
cityCell.Items.AddRange(dict[state].ToArray());
cityCell.Value = city;
}
注意:这会设置DropDowns中的值,但每次更改后您需要在状态列中修改该行中的城市列!因此,将 Dictionary
persistent 移至类级别并创建一个可以在调用后调用的小函数将是一个好主意。
Note: This sets up the values in the DropDowns, but after each change in a 'State' column you will need to adapt the 'Cities' column in that row! Therefore it would be a good idea to make the Dictionary
persistent by moving it to class level and to create a little function you can call after such a change.
以下是此功能的示例:
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
setCities(e.RowIndex, dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
}
void setCities(int row, string state)
{
DataGridViewComboBoxCell cityCell =
(DataGridViewComboBoxCell)(dataGridView1.Rows[row].Cells[1]);
cityCell.Items.Clear();
cityCell.Items.AddRange(dict[state].ToArray());
cityCell.Value = cityCell.Items[0];
}
请注意,新的 cityCell.Value
仅在离开已编辑的单元格后才显示!
Note that the new cityCell.Value
shows up only after leaving the edited cell!
最后的注释:如果您真的想在每个状态下仅显示一行,则需要更改循环填充 DGV
来循环遍历表而不是遍历 dict.Keys
集合:
Final note: If you really want to display only one row per state, you need to change the loop that fills the DGV
to loop not over your table but over the dict.Keys
collection:
for (int i = 0; i < dict.Keys.Count; i++)
{
string state = dict.Keys.ElementAt(i);
string city1 = dict[state][0].ToString();
dataGridView1.Rows.Add();
DataGridViewComboBoxCell stateCell =
(DataGridViewComboBoxCell)(dataGridView1.Rows[i].Cells[0]);
stateCell.Items.AddRange(dict.Keys.ToArray());
stateCell.Value = state;
DataGridViewComboBoxCell cityCell =
(DataGridViewComboBoxCell)(dataGridView1.Rows[i].Cells[1]);
cityCell.Items.AddRange(dict[state].ToArray());
cityCell.Value = city1;
}
这篇关于Datagridview combobox每一行的列值不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!