views:

50

answers:

1

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 tables named "books" and "categories" with the following schema:

books(id, title, catId)
categories(id, catName)

Obviously the "catId" field in the "books" table is a foreign key and specifies a category that a book belongs to.

I have created the necessary LinQ to Sql classes and created the necessary bindingSource object. What I'm trying to do is to display all the books in a DataGridView object. I want it to have a column named "Category" which is of type DataGridViewComboBoxColumn containing all existing categories and for each book displays the category that the specific book belongs to. The user can reassign a book's category by choosing another category in the combo box.

I've managed to do exactly what I want with a ComboBox and it works just as I want. But when it comes to the DataGridView I just can't figure it out.

Any help would be greatly appreciated I've spent days to figure something out but no luck so far :-(

+1  A: 

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
Thomas Levesque
WOW I just can't believe it! It worked just the way I wanted. Thank you so much!The thing is I spent countless hours playing with these properties. I set them all this way too but none worked.Guess the problem I had was with the "DataSource" I set.I used a LinQ to select them and it didn't work. Guess the "ToList()" method's doing all the magic! :-)Care to explain what exactly we're doing here?I kind of always confuse "DisplayMember" with "DataPropertyName". :-pAgain thank you so much for your precise answer :-)
M2X
DataPropertyName is a common property to all DGV columns. It is the name of the data object member represented by that column. DisplayMember/ValueMember are specific to DataGridViewComboBoxColumn. The represent, respectively, the member used to display the object in the UI, and the member that is used as the "selected value" of the combo (note that these properties also exist for other controls like ComboBox or ListBox)
Thomas Levesque
Thanks for the explanation!"DataPropertyName" is set to "catId" which is a property of a "book".The "categoryColumn"'s "DataSource" is assigned to "Categories".How does that work exactly? AFAIK we're telling the compiler that the "categoryColumn" is bound to "Books"'s "catId". But where is the link between the "categoryColumn" and the "Books"?!
M2X
There's no link between `categoryColumn` and `Books`. Each row of the DGV represents a book. The value of the cell in the category column is the `catId` property of the book. And it is represented by a `ComboBox` whose selected value is `catId`
Thomas Levesque
Aha! I see! Thank you so much for your help pal :-)
M2X