本文介绍了没有getter setter methid的Grid View抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 让我先解释一下实施情况。以下是我正在使用的基类 public class DataClass { public string 名称; public int 成员; } 以下将数据提供给GridView 公共类DataFields {公共静态列表< DataClass > getData() { List < DataClass > dataList = new List < DataClass > (); for(int i = 1; i < 10; i ++) { DataClass data = new DataClass() ; data.name = H + i; data.member = I; dataList.Add(数据); } return dataList; } } 以下是aspx页面类 受保护 void Page_Load ( object sender,EventArgs e) { if (!IsPostBack) { GridView1.DataSource = DataFields.getData(); GridView1.DataBind(); } } 虽然这是一个简单的实现我得到了gridview没有任何属性或属性错误。但是我通过以下方式实现DataClass解决了这个问题 public class DataClass { public string name { get ; set ; } public int member { get ; set ; } } 我想知道为什么这次没有错误因为我将这两个变量用作公共偶数之前。我知道在所有情况下都没有必要使用getter和setter方法。但我想知道为什么我们必须在上述情况下使用。解决方案 因为带有id'GridView1'的GridView的数据源没有任何属性或者从中生成列的属性。要为gridview生成列,您需要数据源中的属性。 GridView的DataBind()方法始终检查数据源中的属性以绑定网格视图并创建列。 / blockquote> 如果你使用像T这样的自定义类型(例如:DataClass),你应该属性,而不是字段 Hi,Let me explain the implementation first. Below is the base classes that I am usingpublic class DataClass { public string name; public int member; }Following will give the data to the GridViewpublic class DataFields { public static List<DataClass> getData() { List<DataClass> dataList = new List<DataClass>(); for (int i = 1; i < 10; i++) { DataClass data = new DataClass(); data.name = "H" + i; data.member = i; dataList.Add(data); } return dataList; } }Following is the aspx page classprotected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GridView1.DataSource = DataFields.getData(); GridView1.DataBind(); } }Though this is a simple implementation I got "gridview did not have any properties or attributes" error. But I solved this by implementing the DataClass in the following waypublic class DataClass { public string name { get; set; } public int member { get; set; } }I want to know why there is no error this time as I m using both the variables as public even before. I know it is not necessary to use the getter and setter method in all case. But I want to know why do we have to use under the above case. 解决方案 Because the data source for GridView with id 'GridView1' did not have any properties or attributes from which to generate columns. To generate columns to gridview you need to properties in data source.DataBind() method of GridView always checks property in data source to bind grid view and creates columns.If you are using Custom Types like T ( ur example: DataClass ), You should Properties only , not the fields 这篇关于没有getter setter methid的Grid View抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-11 11:01