0

So I'm attempting to code a fully-automated DnD sheet, and for the Extra Bonus section I figured I'd use a DataGridView where the user can freely add new bonuses to their character (including its equipped state, its name, its affected value, and its bonus value).

Here's my class. The 3rd property is an enum with some placeholder values for the sake of testing it.

    public class ExtraBonus
{
    public bool isEquipped { get; set; }
    public string bonusName { get; set; }
    public AffectedValues.values affectedValue { get; set; }
    public string bonus { get; set; }
}

Here's my list with some more pre-entered placeholder values:

        public static List<ExtraBonus> list = new List<ExtraBonus>()
    {
        new ExtraBonus { isEquipped = true, bonusName = "Divine Smite", affectedValue = AffectedValues.values.Heavy, bonus = "+1d6"},
        new ExtraBonus { isEquipped = false, bonusName = "Hexblade", affectedValue = AffectedValues.values.None, bonus = "+3"},
        new ExtraBonus { isEquipped = true, bonusName = "Hex", affectedValue = AffectedValues.values.Light, bonus = "+1d8"}
    };

And here is where the list is bound to my DataGridView:

            var bindingList = new BindingList<ExtraBonus>(ExtraBonusList.list);
        var source = new BindingSource(bindingList, null);
        ExtraBonusesGridView.DataSource = source;

With this I currently get the following (don't mind the formatting):

This seems to work but the program returns an error message when it's attempting to fill the third column. Says the value in DataGridViewComboBoxCell isn't valid (paraphrasing because the actual error log is in french).

I've been looking up all kinds of tutorials and I just can't find anything for this. I want my 3rd property (an enum of possible values that can be affected) to be bound to this ComboBox column where the user can freely select which value gets affected by this bonus.

How can I do this?

Thanks!

BasuKun
  • 31
  • 4
  • The DataSource of your ComboBoxColumn should be, e.g., `(ExtraBonusesGridView.Columns["affectedValue"] as DataGridViewComboBoxColumn).DataSource = Enum.GetValues(typeof(AffectedValues));`, where `AffectedValues` is the enumerator. If it's not an enumerator, post the definition of `AffectedValues`. – Jimi Nov 25 '20 at 17:08
  • BTW, you don't need both a BindingList and a BindingSource, you can just set `ExtraBonusesGridView.DataSource = new BindingSource(ExtraBonusList.list, null);`. It's not clear why `ExtraBonusList` has a `list` property. – Jimi Nov 25 '20 at 17:18
  • This works wonderfully, thank you! And yes you're most likely right. I'm still learning the ropes of DataGridViews, and another tutorial seemed to suggest that I should always use a BindingList, because it provides more options than directly binding the list or something? In any case, thanks a lot for your answer. :) – BasuKun Nov 25 '20 at 17:31
  • The BindingSource creates a BindingList internally. You could also set `ExtraBonusesGridView.DataSource = new BindingList(ExtraBonusList.list);`. You usually add a BindingSource in between when you need to bind other Controls that use the same source of data: it provides better DataBindings functionality. In this case, it's probably better to store the BindingSource as a Field for easier access. – Jimi Nov 25 '20 at 17:35
  • I see, this makes sense! Thanks, I'll go ahead and optimize my code then. – BasuKun Nov 25 '20 at 17:39

0 Answers0