将对象的属性绑定到datagridview为DataGridViewComboBoxColumn

时间:2018-08-08 09:53:36

标签: c# winforms datagridview

我想将对象的属性绑定到datagridview为DataGridViewComboBoxColumn,以便用户仅选择特定值。 自动生成必须为真,因为我将其用于多个对象。

我试图捕获列并将其更改为DataGridViewComboBoxColumn,但它只具有getter方法,没有设置方法。

这是我想要的样品。

  public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        List<GridClass> BindLxxxist = new List<GridClass>();
        for (int i = 1; i <= 5; i++)
        {
            GridClass g = new GridClass(i.ToString());
            BindLxxxist.Add(g);
        }
        dataGridView1.DataSource = new BindingList<GridClass>(BindLxxxist);
        List<string> idsList=new   List<string>() {"id1","id12"};
        //how to make id column appears as DataGridViewComboBoxColumn to make user select from idsList only.
    }

}
public class GridClass
{
    [DisplayName("ID")]
    public string Id { get; set; }

    [DisplayName("First Name")]
    public string FirstName { get; set; }
    [ListBindable(BindableSupport.Yes)]

    [DisplayName("Last Name")]
    public string LastName { get; set; }



    public GridClass(string val)
    {
        Id = val;
        FirstName = "FirstName" + val;
        LastName = "LastName" + val;


    }


}

0 个答案:

没有答案