这是我第一次这样做,所以我需要一些帮助,
我有这个代码:
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();
}
设计中的:
<asp:Button ID="btnProcess" runat="server" Text="Process"
onclick="btnProcess_Click" />
<asp:GridView ID="gvOriginal" runat="server"></asp:GridView>
但是试图运行网络应用程序,我收到以下错误:
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.
我还需要做任何其他工作吗?
答案 0 :(得分:4)
数据绑定依赖于使用属性而非字段,因为您收到的错误消息表明。您可以轻松更改代码,以便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; }
}
如果你进行了改变,一切都应该有效。
请注意,属性和公共字段之间存在许多细微差别。属性实际上是围绕方法的语法糖,因此public string Placa {get;set;}
将转换为类似于:
private string _placa;
public string GetPlaca() { return _placa; }
public void SetPlaca(string value) { _placa = value; }
至于方法和领域之间的差异,这可能超出了这个问题的范围。
答案 1 :(得分:1)
您可以绑定到列表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;}
}