Skip to content

InitializeDisplayMemberPropertyDescriptor and InitializeValueMemberPropertyDescriptor give misleading error message #12810

Closed
@mpolicki

Description

.NET version

9.0.102

Did it work in .NET Framework?

Not tested/verified

Did it work in any of the earlier releases of .NET Core or .NET 5+?

No response

Issue description

In DataGridViewComboBoxCell, the methods InitializeDisplayMemberPropertyDescriptor and InitializeValueMemberPropertyDescriptor throw ArgumentExceptions with the message SR.DataGridViewComboBoxCell_FieldNotFound.

if (displayMemberProperty is null)
{
throw new ArgumentException(string.Format(SR.DataGridViewComboBoxCell_FieldNotFound, displayMember));

if (valueMemberProperty is null)
{
throw new ArgumentException(string.Format(SR.DataGridViewComboBoxCell_FieldNotFound, valueMember));

I have not been able to find the source string for the message, but it is "Field called {0} does not exist.", with the appropriate substitution for "{0}".
It is my understanding that binding happens to properties, not fields, and thus the error message should say "Property called {0} does not exist.". The current error message is especially confusing to newcomers to data binding who try to bind to fields since it claims exactly the opposite of what's true (e.g. you try to bind to a field called Item1, and you get an error saying "Field called Item1 does not exist.").

Steps to reproduce

Create a new Windows Forms project and use the following code.

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            DataGridViewComboBoxColumn column = new();
            DataGridView dataGridView = new();
            FlowLayoutPanel flowLayoutPanel = new();
            Controls.Add(flowLayoutPanel);
            flowLayoutPanel.Controls.Add(dataGridView);
            dataGridView.Columns.Add(column);
            column.DataSource = new ValueTuple<string, int>[] { new("One", 1) };
            // The following line throws System.ArgumentException: 'Field called Item1 does not exist.'
            column.DisplayMember = nameof(ValueTuple<string, int>.Item1);
            column.ValueMember = nameof(ValueTuple<string, int>.Item2);
        }
    }

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions