Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

InitializeDisplayMemberPropertyDescriptor and InitializeValueMemberPropertyDescriptor give misleading error message #12810

Closed
mpolicki opened this issue Jan 18, 2025 · 4 comments · Fixed by #12826
Assignees
Labels
🚧 work in progress Work that is current in progress
Milestone

Comments

@mpolicki
Copy link

.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);
        }
    }
@mpolicki mpolicki added the untriaged The team needs to look at this issue in the next triage label Jan 18, 2025
@Tanya-Solyanik
Copy link
Member

Good point! I think this message refers to https://learn.microsoft.com/dotnet/api/system.windows.forms.bindingmemberinfo.bindingfield?view=windowsdesktop-9.0, but we are actually searching for a property with the given name.

@merriemcgaw
Copy link
Member

@LeafShi1 / @ricardobossan can you please update the string to reflect "property"? We've confirmed that "property" is the right value.

@merriemcgaw merriemcgaw removed the untriaged The team needs to look at this issue in the next triage label Jan 22, 2025
@Tanya-Solyanik Tanya-Solyanik added this to the .NET 10.0 milestone Jan 22, 2025
@dotnet-policy-service dotnet-policy-service bot added the 🚧 work in progress Work that is current in progress label Jan 22, 2025
ricardobossan pushed a commit to ricardobossan/winforms that referenced this issue Jan 24, 2025
…gested, ensuring it reflects the correct value.

Fixes dotnet#12810

- The original resource file used the term "field" instead of "property," which caused confusion in the context where we were searching for a property with a given name.

- Updated the resource string to replace "field" with "property" as suggested, ensuring it reflects the correct value.

- None

- No

- Minimal

- Manual

- `10.0.100-alpha.1.25064.3`
@Olina-Zhang
Copy link
Member

Verified in the latest .NET 10 SDK build: 10.0.100-preview.2.25105.6, it was fixed: now the error message for displayMenber and valueMember of dataGridViewComboBoxColumn uses property string:

Image Image

@Olina-Zhang Olina-Zhang modified the milestones: .NET 10.0, 10.0 Preview2 Feb 6, 2025
@Syareel-Sukeri
Copy link
Contributor

Verified with .NET SDK 10.0 P1 test pass build, it was fixed as above testing result.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
🚧 work in progress Work that is current in progress
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants