Problem with data binding to DevExpress XtraGrid

2019-08-26 16:44发布

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

1条回答
女痞
2楼-- · 2019-08-26 17:11

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.

查看更多
登录 后发表回答