winform中,dataGridView中绑定一个包括Name属性的数据源,引起名字冲突

杀了这个男孩 2020-02-13 05:08:11
一个窗体,只包括一个dataGridView。
设计代码

namespace WindowsFormsApp1
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows 窗体设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.Id = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Name = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Price = new System.Windows.Forms.DataGridViewTextBoxColumn();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.AllowUserToDeleteRows = false;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.Id,
this.Name,
this.Price});
this.dataGridView1.Location = new System.Drawing.Point(74, 30);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.ReadOnly = true;
this.dataGridView1.RowTemplate.Height = 23;
this.dataGridView1.Size = new System.Drawing.Size(522, 297);
this.dataGridView1.TabIndex = 0;
//
// Id
//
this.Id.HeaderText = "Id";
this.Id.Name = "Id";
this.Id.ReadOnly = true;
//
// Name
//
this.Name.HeaderText = "Name";
this.Name.Name = "Name";
this.Name.ReadOnly = true;
//
// Price
//
this.Price.HeaderText = "Price";
this.Price.Name = "Price";
this.Price.ReadOnly = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.DataGridViewTextBoxColumn Id;
private System.Windows.Forms.DataGridViewTextBoxColumn Name;
private System.Windows.Forms.DataGridViewTextBoxColumn Price;
}
}



仅从这里就看出来,代码里有双重含义的.this.Name
逻辑代码也出来好了

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
var bookList=new List<Book>()
{
new Book{Id=1,Name = "Good Story",Price = 335.2},
new Book{Id=2,Name = "Better Story",Price = 335.2},
new Book{Id=2,Name = "Programer's Story'",Price = 335.2},
new Book{Id=3,Name = "Normal Story",Price = 335.2},
new Book{Id=4,Name = "Good Story",Price = 335.2}
};

dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = bookList;
}
}
}


编译出错:CS0029 无法将类型“string”隐式转换为“System.Windows.Forms.DataGridViewTextBoxColumn” WindowsFormsApp1 C:\Users\qlzcl\source\repos\WindowsFormsApp1\WindowsFormsApp1\Form1.Designer.cs 78 活动
78行就是设计文件中的this.Name="Form1",因为前面已经指定了一个this.Name是列属性。

我想问一下,这是不是一个bug,当然完全可以数据源不要用name属性就可以了,但是这个问题很奇怪吧
...全文
200 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
jimmy212 2020-02-14
  • 打赏
  • 举报
回复
这有什么奇怪的,你的name覆盖了原来name的定义,相当于有2个name 你定义一个变量2种类型,怎么能不报错呢
  • 打赏
  • 举报
回复
提示是正确的,该换思路去看待问题,之前遇到过,去掉就可以了。

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧