Problem with data binding to DevExpress XtraGrid

两盒软妹~` 提交于 2019-12-12 02:35:15

问题


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:


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.



来源:https://stackoverflow.com/questions/5486408/problem-with-data-binding-to-devexpress-xtragrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!