【问题标题】:two comboboxes in datagridviewdatagridview 中的两个组合框
【发布时间】:2010-10-16 02:05:24
【问题描述】:

我正在使用 windowsforms 中的 datagridview 中的组合框进行 delaeaing 假设我在一个 datagridview 中有两个组合框,所以我的问题是
如何根据此datagridview中另一个comboboxcolum的选定值填充datagridview中的comboboxcolumn

【问题讨论】:

  • 您的意思是网格中的每一行都有两个组合框列吗?
  • 请回答,因为这个问题很紧急,谢谢^_^

标签: c# datagridview combobox sqldatareader


【解决方案1】:

好吧,我想我有点晚了,但是您应该做的是在您的列上附加一个值更改事件处理程序。当您选择一个新值时,处理程序应该触发,然后您使用传递给函数的参数来确定更改了哪一行并更改该行中的第二个组合框。我会提供一个例子,但我几乎可以肯定你无论如何都不会回来看这个答案。

编辑: 没想到你真的会为此卷土重来,但这是实现动态组合框的一种方法。我不能说我对这个答案感到完全自豪,但是因为它不适用于数据绑定(您必须自己添加每一行并设置正确的初始值。)但至少这个解决方案可以为您指明正确的方向或者一个新的。

public partial class DualComboBoxGridViewForm : Form
{
    private Dictionary<Country, List<City>> locations;

    public DualComboBoxGridViewForm()
    {
        InitializeComponent();
        InitializeLocations();

        dataGridView1.Rows.Add(new DataGridViewRow());
    }

    private void InitializeLocations()
    {
        // Loading these from a database would be highly recommended, I 
        // just did it like this with a dictionary to it would be easier 
        // to show.
        locations = new Dictionary<Country, List<City>>();

        List<City> americanCities = new List<City>();
        americanCities.Add(new City { ID = 0, Name = "Please Select A City" });
        americanCities.Add(new City { ID = 1, Name = "Boston" });
        americanCities.Add(new City { ID = 2, Name = "New York" });

        List<City> japaneseCities = new List<City>();
        japaneseCities.Add(new City { ID = 0, Name = "Please Select A City" });
        japaneseCities.Add(new City { ID = 1, Name = "Tokyo" });
        japaneseCities.Add(new City { ID = 2, Name = "Kyoto" });

        locations.Add(new Country { ID = 0, Name = "Please Select A Country" }, new List<City>());
        locations.Add(new Country { ID = 1, Name = "USA" }, americanCities);
        locations.Add(new Country { ID = 2, Name = "Japan" }, japaneseCities);
    }

    private void InitializeDataGridView()
    {
        dataGridView1.AutoGenerateColumns = false;
        dataGridView1.EditingControlShowing += dataGridView1_EditingControlShowing;

        // Create columns
        DataGridViewTextBoxColumn eventNameColumn = new DataGridViewTextBoxColumn();
        eventNameColumn.HeaderText = "Event";

        DataGridViewComboBoxColumn countryComboBox = new DataGridViewComboBoxColumn();
        countryComboBox.Name = "Country";
        countryComboBox.HeaderText = "Country";
        countryComboBox.ValueMember = "ID";
        countryComboBox.DisplayMember = "Name";
        foreach (Country country in locations.Keys)
        {
            countryComboBox.Items.Add(country);
        }

        DataGridViewComboBoxColumn cityComboBox = new DataGridViewComboBoxColumn();
        cityComboBox.Name = "City";
        cityComboBox.HeaderText = "City";
        cityComboBox.ValueMember = "ID";
        cityComboBox.DisplayMember = "Name";

        dataGridView1.Columns.Add(eventNameColumn);
        dataGridView1.Columns.Add(countryComboBox);
        dataGridView1.Columns.Add(cityComboBox);
    }

    // Triggers when a column enters edit mode (new value not yet assigned).
    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs args)
    {
        // we only want to change the city box if a country value is changed
        if (dataGridView1.CurrentCell.ColumnIndex == dataGridView1.Columns["Country"].Index)
        {
            ComboBox countryBox = args.Control as ComboBox;
            countryBox.SelectedIndexChanged += countryComboBox_SelectedIndexChanged;
        }
    }

    private void countryComboBox_SelectedIndexChanged(object sender, EventArgs args)
    {
        ComboBox box = sender as ComboBox;

        DataGridViewComboBoxCell cityCell = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[dataGridView1.Columns["City"].Index] as
            DataGridViewComboBoxCell;
        cityCell.Items.Clear();
        foreach (City city in locations[box.SelectedItem as Country])
        {
            cityCell.Items.Add(city);
        }

        if (cityCell.Items.Count > 0)
        {
            cityCell.Value = cityCell.Items[0];
        }

        // Remove event handler to prevent memory leak
        box.SelectedIndexChanged -= countryComboBox_SelectedIndexChanged;
    }
}

public class Country
{
    public int ID { get; set; }
    public string Name { get; set; }
}

public class City
{
    public int ID { get; set; }
    public string Name { get; set; }
}

【讨论】:

  • @Denjamin 我回来了,我在等你的例子
  • @mohammedsameeh 我很抱歉没有找到使数据绑定工作的方法,但这非常棘手,因为您需要在数据绑定时动态分配城市组合框。您可以使用虚拟模式执行此操作,但只有在列未绑定时才能触发事件。一种解决方法是创建绑定到实际数据的隐藏列,并使一些可见的未绑定列显示下拉列表(只需使用 EditingControlShowing 等事件来更新隐藏的数据绑定列。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-12
  • 1970-01-01
  • 2016-03-31
  • 1970-01-01
  • 1970-01-01
  • 2014-05-29
  • 2011-02-25
相关资源
最近更新 更多