简体   繁体   English

C#DataGridView +绑定列表-单元格类型更改

[英]C# DataGridView + Binding List - Cell Type Change

I have a binding list which I use as a datasource for my datagridview. 我有一个绑定列表,用作我的datagridview的数据源。 I use iNotify to update my list whenever the cell value has changed. 每当单元格值更改时,我都会使用iNotify更新我的列表。 Which then writes to an Access Database. 然后将其写入Access数据库。 However I need a way to validate the input and make it easy for users to know what type of data is expected. 但是,我需要一种方法来验证输入并使其易于用户知道所需的数据类型。

IE My "Shift" Column needs 3 shift options. IE浏览器的“班次”列需要3个班次选项。 I suspect I can do this with a combobox, but I'm not quite sure how to change the celltype, and still have it update my Binding List on update, by just binding my list as the datagridview's datasource. 我怀疑我可以用组合框来做到这一点,但是我不太确定如何更改单元格类型,并且仍然只是通过将列表绑定为datagridview的数据源来使它在更新时更新我的​​绑定列表。

Any hints would be awesome. 任何提示都会很棒。

Are you looking for something like this? 您是否正在寻找这样的东西?

    private void Form1_Load(object sender, EventArgs e)
    {
        BindingList<Shift> bindingList = new BindingList<Shift>();           
        bindingList.Add(new Shift(ShiftType.SHIFT1));
        bindingList.Add(new Shift(ShiftType.SHIFT2));
        bindingList.Add(new Shift(ShiftType.SHIFT3));
        bindingList.Add(new Shift(ShiftType.SHIFT1));

        var ShiftColumn = new DataGridViewComboBoxColumn();
        dataGridView1.Columns.Add(ShiftColumn);

        dataGridView1.AutoGenerateColumns = false;
        dataGridView1.DataSource = bindingList;
        ShiftColumn.DataPropertyName="shiftType";
        ShiftColumn.DataSource = new List<ShiftType> { ShiftType.SHIFT1, ShiftType.SHIFT2, ShiftType.SHIFT3 };


    }

}
class Shift
{

    public ShiftType shiftType { get; set; }


    public Shift(ShiftType shiftType)
    {
        this.shiftType = shiftType;

    }

}
enum ShiftType
{
    SHIFT1 ,
    SHIFT2,
    SHIFT3 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM