winForm DataGridView实现列的关联操作问题
本人在网上查到以下资料:
DataGridView中有两列是DataGridViewComboBoxColumn类型的,要用第一个下拉列表控制第二个,有点类似二级联动的效果,在这里记录一下。
首先要在DataGridView的EditingControlShowing事件中给下拉框动态添加事件,如下:
private void dataGridView1_EditingControlShowing(object sender,DataGridViewEditingControlShowingEventArgs e)
{
//给指定列的下拉框添加SelectedIndexChanged事件
if (dataGridView1.CurrentCell.RowIndex != -1 && dataGridView1.CurrentCell.ColumnIndex == 1)
((ComboBox)e.Control).SelectedIndexChanged += new EventHandler(v2_SelectedIndexChanged);
}
在这里就可以写事件了
private void v2_SelectedIndexChanged(object sender, EventArgs e)
{
//这里写要处理的代码。。。最后要把事件删除,不然会出问题
((ComboBox)sender).SelectedIndexChanged -= new EventHandler(v2_SelectedIndexChanged);
}
现在的问题是,当改变第一个下拉框的值时,第二个下拉框要相应改变,但这里怎么获取到第二个下拉框相应的行和列信息?
没有该信息就没法设置第二个下拉框的内容!!!
[解决办法]
下拉框的的数据源你需要单独保存,无法从DataGridView中获取。
[解决办法]
下拉框实例在整个DataGridVeiw内是唯一的,并不会为每一行创建下拉框并保存其信息。
因此:
1、SelectedIndexChanged += new EventHandler(v2_SelectedIndexChanged) 不要在EditingControlShowing处添加,而应在创建DataGridViewComboBoxColumn处添加(这也正是你这里需要在SelectedIndexChanged中删除事件的原因)
2、要对第二个下拉框进行更改,直接对创建第二个DataGridViewComboBoxColumn时的实例引用进行操作。至于行号,相应事件的e.RowIndex就是了
[解决办法]
if (e.ColumnIndex == 2)
{
if (e.RowIndex > 0)
{
DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
}
}
[解决办法]
1. 下拉框列的数据源属于那个 列(Column)
2. 默认情形下, 列共享同一个 View, (对于 DataTable, 就是 DataView)
有一点需要注意:
如果你要做类似省市地区那样的关联, 对于第二个列的过滤条件需要加载对应的单元格上,
而不能直接设置 列 上数据源的过滤条件.
[解决办法]
一列只有一个数据源。
如果是直接绑定就算了,找其他地方解决吧。
[解决办法]
实在抱歉,之前是没经过深思熟虑外加纸上谈兵了。
public partial class Form1 : Form { public class ComboBoxItem { private int _Value; public int Value { get { return _Value; } set { _Value = value; } } private String _Name; public String Name { get { return _Name; } set { _Name = value; } } public ComboBoxItem(int value, String name) { _Value = value; _Name = name; } } const int ProvinceColumnIndex = 0; //“省”列列号 const int CityColumnIndex = 1; //“市”列列号 readonly List<ComboBoxItem> listChina = new List<ComboBoxItem> { new ComboBoxItem(0, "广东省"), new ComboBoxItem(1, "江苏省") }; readonly List<ComboBoxItem> listGuangdong = new List<ComboBoxItem> { new ComboBoxItem(0, "广州市"), new ComboBoxItem(1, "中山市") }; readonly List<ComboBoxItem> listJiangsu = new List<ComboBoxItem> { new ComboBoxItem(0, "盐城市"), new ComboBoxItem(1, "苏州市") }; public Form1() { InitializeComponent(); DataGridViewComboBoxColumn columnProvince = new DataGridViewComboBoxColumn(); columnProvince.Name = "Province"; columnProvince.HeaderText = "Province"; columnProvince.DataSource = listChina; columnProvince.ValueMember = "Value"; columnProvince.DisplayMember = "Name"; //dataGridView1.Columns.Add(columnProvince); dataGridView1.Columns.Insert(ProvinceColumnIndex, columnProvince); DataGridViewComboBoxColumn columnCity = new DataGridViewComboBoxColumn(); columnCity.Name = "City"; columnCity.HeaderText = "City"; //dataGridView1.Columns.Add(columnCity); dataGridView1.Columns.Insert(CityColumnIndex, columnCity); dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing); } void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if (dataGridView1.CurrentCellAddress.Y >= 0 && dataGridView1.CurrentCellAddress.X == ProvinceColumnIndex) { ComboBox comboBox = (ComboBox)e.Control; comboBox.SelectedIndexChanged -= new EventHandler(Form1_SelectedIndexChanged); //避免组合框复用时重复订阅事件 comboBox.SelectedIndexChanged += new EventHandler(Form1_SelectedIndexChanged); } } void Form1_SelectedIndexChanged(object sender, EventArgs e) { if (dataGridView1.CurrentCellAddress.Y >= 0 && dataGridView1.CurrentCellAddress.X == ProvinceColumnIndex) { ComboBox comboBox = dataGridView1.EditingControl as ComboBox; if (comboBox == null || comboBox.SelectedItem == null) { return; } String selected = ((ComboBoxItem)comboBox.SelectedItem).Name; if (dataGridView1.CurrentCell.Value != null && dataGridView1.CurrentCell.Value.ToString() == selected) { return; //如果已存值与新值相同,则不进行任何操作 } List<ComboBoxItem> list; switch (selected) { case "广东省": list = listGuangdong; break; case "江苏省": list = listJiangsu; break; default: throw new NotImplementedException(); } //更改子列组合框数据源 DataGridViewComboBoxCell cellCity = (DataGridViewComboBoxCell)dataGridView1.Rows[dataGridView1.CurrentCellAddress.Y].Cells[CityColumnIndex]; cellCity.DataSource = list; cellCity.ValueMember = "Value"; cellCity.DisplayMember = "Name"; } } }