0

i have my class with 4 public int properties. What i'm trying to do is to bind List to BindingSource as DataSource and that BindingSource to DataGridView. Everything works perfectly, now i'm trying to "translate" cells value to something else. For example i have:

List<MyClass> list = new List<MyClass>()
list.Add(new MyClass(1,2,3,4))
list.Add(new MyClass(0,2,3,4))

So in DataGridView it would look like this:

----------------
Row1 1|2|3|4
----------------
Row2 0|2|3|4
----------------

But what i'm trying to do is to be something like this:

----------------
Row1 "Dog"|2|3|4
----------------
Row2 "Cat"|2|3|4
----------------

As you see "0" value from my class is visible as "Cat" and "1" as "Dog" in DataGridView . I want it to be ComboBoxCell and to be working in two directions. Changing "Dog" to "Cat" should change value in the underlying List from "1" to "0". What is the best, cleanest way to do it? Thanks for any help.

Marduk
  • 359
  • 4
  • 13

1 Answers1

0

Every DataGridView has a collection of column definitions. Within that list there are currently four textBoxColumns. That's why you see

----------------
Row1 1|2|3|4
----------------
Row2 0|2|3|4
----------------

You now have to change the column definition for your first column to a DataGridViewComboBoxColumn. After that you can set a dataSource for this column (eg: a new BindingList containing Dog, Cat aso) and bind your sourceColumn to DataPropertyName

// short demo code, MUST BE ADAPTED
dgvCOMBOBOXCOLUMN.DataSource = YOUR_CATS_DOGS_BS;
dgvCOMBOBOXCOLUMN.DisplayMember = COLUMNNAME_OF_BS_TO_SHOW;
dgvCOMBOBOXCOLUMN.Value = COLUMNNAME_OF_BS_WITH_ACCORDING_IDs;
dgvCOMBOBOXCOLUMN.DataPropertyName = COLUMNNAME_OF_MYCLASS_TO_BIN;

Of course you can make those settings within the VS designer, which is recommended!

Pilgerstorfer Franz
  • 8,303
  • 3
  • 41
  • 54
  • Wow, that was so easy. I've lost so much time with no effects and thanks to you it works perfecly now. Thanks very much! – Marduk Sep 16 '12 at 21:28
  • If you found my answer helpful you may consider _upvoting_ it and/or _marking it as best answer_! See [FAQ](http://stackoverflow.com/faq#reputation) for detailed information – Pilgerstorfer Franz Sep 17 '12 at 06:07