数据网格视图中具有自己的绑定源的每一行(对象)



我正在用C#(.NET 4.0)编写一个小应用程序。我有一个数据网格视图,每行代表一个对象。我想要一个允许选择该对象特定属性的组合框列。

例:

public class Car
{
   public String Make {get; set;}
   public BindingList<String> AllColors {get; set;}
   public int SelectedColorIndex {get; set;}
}

每行表示一个 Car 对象。每个(不同的)汽车对象都有自己的可能颜色选择(AllColors)。我想要一个列,您可以在其中通过从AllColors中选择一种颜色来设置SelectedColorIndex(AllColors特定于每个Car对象)。

注意:我编造了这个例子,但它描述了我想要完成的事情。

我怎样才能做到这一点? 我发现的唯一解决方案是在 datagridview 之外有一个特定的组合框,您可以使用它更改所选行。在行输入事件中,我将绑定源的数据源更改为当前的"AllColors"。

感谢您的时间和答案。

下面是

一个工作示例的代码,其中可用的颜色被筛选为绑定对象的 AllColors 列表中提供的颜色。

魔术发生在CellBeginEditCellEndEdit事件处理程序中 - 我们为每个组合框单元格提供绑定行中的列表,然后在退出时重置它。

这依赖于的一件事是拥有一个包含所有颜色的主列表 - 没有办法解决这个问题。

此外,我还为新行需要默认值的情况添加了处理。我所做的只是将所选索引默认设置为一。当然,这在现实世界中是行不通的,你需要更聪明的东西!MSDN 上介绍了DefaultValuedNeeded事件。

public partial class Form1 : Form
{
    private BindingSource cars;
    private BindingSource masterColors;
    public Form1()
    {
        InitializeComponent();
        masterColors = new BindingSource();
        masterColors.Add(new CarColor{Name = "Blue", Index = 1});
        masterColors.Add(new CarColor{Name = "Red", Index = 2});
        masterColors.Add(new CarColor { Name = "Green", Index = 3 });
        masterColors.Add(new CarColor { Name = "White", Index = 4 });
        BindingList<CarColor> fordColors = new BindingList<CarColor>();
        fordColors.Add(new CarColor{Name = "Blue", Index = 1});
        fordColors.Add(new CarColor{Name = "Red", Index = 2});
        BindingList<CarColor> toyotaColors = new BindingList<CarColor>();
        toyotaColors.Add(new CarColor { Name = "Green", Index = 3 });
        toyotaColors.Add(new CarColor { Name = "White", Index = 4 });
        cars = new BindingSource();
        cars.Add(new Car { Make = "Ford", SelectedColorIndex = 1, AllColors = fordColors });
        cars.Add(new Car { Make = "Toyota", SelectedColorIndex = 3, AllColors = toyotaColors });
        dataGridView1.DataSource = cars;
        dataGridView1.Columns["SelectedColorIndex"].Visible = false;
        //dataGridView1.Columns["AllColors"].Visible = false;
        DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
        col.Name = "AvailableColors";
        col.DataSource = masterColors;
        col.DisplayMember = "Name";
        col.DataPropertyName = "SelectedColorIndex";
        col.ValueMember = "Index";
        dataGridView1.Columns.Add(col);
        dataGridView1.CellBeginEdit += new DataGridViewCellCancelEventHandler(dataGridView1_CellBeginEdit);
        dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);
        dataGridView1.DefaultValuesNeeded += new DataGridViewRowEventHandler(dataGridView1_DefaultValuesNeeded);
    }
    void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
    {
        if (e.ColumnIndex == dataGridView1.Columns["AvailableColors"].Index)
        {
            if (e.RowIndex != dataGridView1.NewRowIndex)
            {
                // Set the combobox cell datasource to the filtered BindingSource
                DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)dataGridView1
                                [e.ColumnIndex, e.RowIndex];
                Car rowCar = dataGridView1.Rows[e.RowIndex].DataBoundItem as Car;
                dgcb.DataSource = rowCar.AllColors;
            }
        }
    }
    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == dataGridView1.Columns["AvailableColors"].Index)
        {
            // Reset combobox cell to the unfiltered BindingSource
            DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)dataGridView1
                            [e.ColumnIndex, e.RowIndex];
            dgcb.DataSource = masterColors; //unfiltered
        }
    }
    void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
    {
        e.Row.Cells["SelectedColorIndex"].Value = 1;
    }
}
public class Car
{
    public String Make { get; set; }
    public BindingList<CarColor> AllColors { get; set; }
    public int SelectedColorIndex { get; set; }
}
public class CarColor
{
    public String Name { get; set; }
    public int Index { get; set; }
}

1 我第一次从 DataGridView FAQ 中学会了如何做到这一点,这是一个由 Mark Rideout 编写的很棒的资源,他是当时Microsoft DataGridView的项目经理。FAQ 中的示例基于另一个组合框进行筛选,并使用数据表,但原理相同。

最新更新