数据绑定到 DevExpress XtraGrid 的问题
我有一个 XtraGrid 放到 Winform 上。我创建了 3 个名为 ID、StartTime 和 EndTime 的未绑定列,并将它们的未绑定类型分别设置为 Int、DateTime 和 DateTime。 我创建了一个类:
public class Data
{
public Data(int id, DateTime startTime, DateTime endTime)
{
this.id = id;
this.startTime = startTime;
this.endTime = endTime;
}
private int id;
private DateTime startTime;
private DateTime endTime;
public int ID
{
get { return id; }
set { id = value; }
}
public DateTime StartTime
{
get { return startTime; }
set { startTime = value; }
}
public DateTime EndTime
{
get { return endTime; }
set { endTime = value; }
}
}
在表单构造函数中,我创建了一个列表,并在运行时将该列表绑定到我的网格控件
List<Data> list = new List<Data>();
list.AddRange(new Data[] {
new Data(1, Convert.ToDateTime("1:00:00 AM"),
Convert.ToDateTime("3:00:00 AM")),
new Data(2, Convert.ToDateTime("8:00:00 PM"),
Convert.ToDateTime("8:30:00 PM")),
new Data(3, Convert.ToDateTime("12:00:00 PM"),
Convert.ToDateTime("1:00:00 AM")),
new Data(4, Convert.ToDateTime("2:00:00 AM"),
Convert.ToDateTime("3:00:00 AM"))
});
gridControl1.DataSource = list;
。当运行应用程序时,我得到一个空网格。不知何故,我在设计时创建的列在运行时没有正确填充数据。我尝试做同样的事情,在设计时没有创建列,并且应用程序使用正确填充的数据运行。我缺少一些东西。
任何调试问题的想法或 解决问题就会非常 赞赏。提前致谢
I have a XtraGrid dropped on to a Winform. I have created 3 unbound columns named ID, StartTime and EndTime and set their unbound types as Int, DateTime and DateTime respectively.
I have created a class:
public class Data
{
public Data(int id, DateTime startTime, DateTime endTime)
{
this.id = id;
this.startTime = startTime;
this.endTime = endTime;
}
private int id;
private DateTime startTime;
private DateTime endTime;
public int ID
{
get { return id; }
set { id = value; }
}
public DateTime StartTime
{
get { return startTime; }
set { startTime = value; }
}
public DateTime EndTime
{
get { return endTime; }
set { endTime = value; }
}
}
In the form constructor I created a List and bind the list to my gridcontrol at runtime
List<Data> list = new List<Data>();
list.AddRange(new Data[] {
new Data(1, Convert.ToDateTime("1:00:00 AM"),
Convert.ToDateTime("3:00:00 AM")),
new Data(2, Convert.ToDateTime("8:00:00 PM"),
Convert.ToDateTime("8:30:00 PM")),
new Data(3, Convert.ToDateTime("12:00:00 PM"),
Convert.ToDateTime("1:00:00 AM")),
new Data(4, Convert.ToDateTime("2:00:00 AM"),
Convert.ToDateTime("3:00:00 AM"))
});
gridControl1.DataSource = list;
When run the application, I get an empty grid. Somehow the columns that I created at design time are not filled correctly with the data at runtime. I try to do the same thing with no columns created at design time and the application run with correctly filled data. I am missing something.
Any ideas to debug the problem or
solve the problem will be very
appreciated. Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将列的 FieldName 属性设置为 ID、StartTime、EndTime(区分大小写!!!)。另外,我建议您移动代码以将网格的数据源设置为表单的加载事件。这应该对你有帮助。
Set the FieldName property of your columns to ID, StartTime, EndTime (Case Sensitively!!!!). Also, I would suggest that you move your code to set the grid's DataSource to the form's Load event. This should help you.