简体   繁体   中英

C# DataGridView + Binding List - Cell Type Change

I have a binding list which I use as a datasource for my datagridview. I use iNotify to update my list whenever the cell value has changed. Which then writes to an Access Database. 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. 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.

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 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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