dotnet / dotnet/winforms

Visual Studio 2022 Winform designer generates code for DataGridView.AutoGenerate = false is incorrect

Open
#13,009 2 comments 0 reactions 0 assignees View on GitHub
area-VSDesigner
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
20h 23m
Merged PRs (30d)
103

Description

### Environment

Microsoft Visual Studio Enterprise 2022 (64-bit) - Current

Project type: Winform .net core 8.0

### .NET version

Winform .net core 8.0 but i belive .Net 9.0 has the same the issuse

### Did this work in a previous version of Visual Studio and/or previous .NET release?

yes.

### Issue description

In Winform Designer, when adding a new DataGridView control, then setting the property ‘DataGridView.AutoGenerate’ becomes ‘false’, the code generated is incorrect:
Instead of generating ‘dataGridView1.AutoGenerate = false’ into the ‘InitializeComponent()’
it declares a new local variant ‘DataGridView1 dataGridView1;’ the adding new variant ‘dataGridView1’ is the same name as the field ‘dataGridView1’ added by the designer before, thus field ‘dataGridView1’ is never initialized => at runtime throw a Null Ex.

I have recorded my screen below the file attachment (I recorded on another computer, but the other VS version (the newest) still met the same issue).

### Steps to reproduce

in VS designer
- Add a datagridview in form.
- Change the property ``GenerateMember` of datagridview from `true` to `false`
- Check the code generated for the datagridview in method InitializeComponent()

https://github.com/user-attachments/assets/3b92bfd3-95c1-4f08-9ad0-fbd3bd400956

### Diagnostics

```text

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.