一個表單,只包括一個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屬性就可以了,但是這個問題很奇怪吧
uj5u.com熱心網友回復:
提示是正確的,該換思路去看待問題,之前遇到過,去掉就可以了。uj5u.com熱心網友回復:
這有什么奇怪的,你的name覆寫了原來name的定義,相當于有2個name你定義一個變數2種型別,怎么能不報錯呢
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/70687.html
標籤:C#
上一篇:asp.net mvc 通過nuget參考了“RazorGenerator”后來又通過nuget卸載了,但是在此系統上未找到自定義工具“RazorGenerat
