本文介绍了数据绑定到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 = 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;

运行应用程序时,我得到一个空格。不知何故,我在设计时创建的列在运行时没有与数据正确填充。我尝试做同样的事情,没有在设计时创建的列,应用程序运行与正确填充的数据。我错过了一些东西


解决方案

将列的FieldName属性设置为ID,StartTime,EndTime案件敏感!!!!)。此外,我建议您移动代码将网格的DataSource设置为窗体的Load事件。这应该可以帮助你。


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.

解决方案

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.

这篇关于数据绑定到DevExpress XtraGrid的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 21:46