简体   繁体   English

将GridView绑定到asp.net C#中的列表

[英]binding a GridView to a List in asp.net c#

This is the first time that I'm doing this, so I need a little bit of help, 这是我第一次这样做,所以我需要一点帮助,

I have this code behind: 我的代码如下:

List<Trucks> FinalListOfTrucks = new List<Trucks>();
    public class Trucks
    {
        public string Placa;
        public string Lock;
        public string Event;
        public DateTime Date;
        public string TipoCamion;
        public string Person;
        public string MissedDate;
    }

    protected void btnProcess_Click(object sender, EventArgs e)
        {
            Trucks item = new Trucks();
            item.Placa = "MA2323";
            item.Lock = "lock1";
            item.Event = "Event1";
            item.Date = DateTime.Now;
            item.TipoCamion = "TRUCK1";
            item.Person = "JULIAN";
            item.MissedDate = "";
            FinalListOfTrucks.Add(item);

            gvOriginal.DataSource = FinalListOfTrucks;
            gvOriginal.DataBind();
}

in design: 在设计中:

<asp:Button ID="btnProcess" runat="server" Text="Process" 
        onclick="btnProcess_Click" />
    <asp:GridView ID="gvOriginal" runat="server"></asp:GridView>

But trying to run the web app, I'm getting the following error: 但是尝试运行Web应用程序时,出现以下错误:

The data source for GridView with id 'gvOriginal' did not have any properties or attributes from which to generate columns. Ensure that your data source has content.

Do I have to do anything else, to make this work? 要进行这项工作,我还需要做其他事情吗?

Databinding relies on using properties rather than fields , as the error message you got indicates. 数据绑定依赖于使用属性而不是field ,如您得到的错误消息所示。 You can easily change your code so that Trucks uses properties instead: 您可以轻松更改代码,以便Trucks使用属性:

public class Trucks
{
    public string Placa { get; set; }
    public string Lock { get; set; }
    public string Event { get; set; }
    public DateTime Date { get; set; }
    public string TipoCamion { get; set; }
    public string Person { get; set; }
    public string MissedDate { get; set; }
}

If you make that change everything should work. 如果您进行了更改,那么一切都会正常进行。

Note that there are a number of subtle differences between properties and public fields. 请注意,属性和公共字段之间存在许多细微的差异。 A property is effectively syntactic sugar around methods, so public string Placa {get;set;} would be transformed into something similar to: 属性实际上是方法周围的语法糖,因此public string Placa {get;set;}将被转换为类似于以下内容的东西:

private string _placa;
public string GetPlaca() { return _placa; }
public void SetPlaca(string value) { _placa = value; }

As for the differences between methods and fields, that's probably beyond the scope of this question. 至于方法和字段之间的差异,可能超出了此问题的范围。

You can bind to lists gridviews, but your class has to use PROPERTIES, not variables. 您可以绑定到列表gridviews,但是您的类必须使用PROPERTIES,而不是变量。

public class Trucks
{
    public string Placa{get;set;}
    public string Lock{get;set;}
    public string Event{get;set;}
    public DateTime Date{get;set;}
    public string TipoCamion{get;set;}
    public string Person{get;set;}
    public string MissedDate{get;set;}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM