C# DataGridViewComboBoxColumn binding problem

后端 未结 2 526
梦如初夏
梦如初夏 2021-02-10 09:25

Hey everyone! I suppose this is my first post on StackOverFlow.com :-)

I\'ve been having this problem for a while. To make it all simple, suppose we have 2 database tabl

相关标签:
2条回答
  • 2021-02-10 10:04

    This should work:

    // create the column (probably better done by the designer)
    DataGridViewComboBoxColumn categoryColumn = ...
    
    
    // bind it
    categoryColumn.DataSource = db.Categories.ToList();
    categoryColumn.DisplayMember = "catName";  // display category.catName
    categoryColumn.ValueMember = "id";         // use category.id as the identifier
    categoryColumn.DataPropertyName = "catId"; // bind the column to book.catId
    
    0 讨论(0)
  • 2021-02-10 10:12

    I managed to do it by overwriting the ToString() method in the concerned class. For you case, you could add in the definition of categories:

    public override string ToString()
    {
       return this.catName
    }
    
    0 讨论(0)
提交回复
热议问题