如何使用List <object []>在WPF中填充DataGrid?

时间:2015-05-18 23:04:07

标签: c# wpf datagrid

我有这个填充列表的方法,它准备填充DataGrid。我想在指定的个性化列中填写此列表中的每个数据,这里是代码:

public List<Object[]> EventsDates(DateTime date1, DateTime date2, int idUser)
{
    int dateStamp1 = Convert.ToInt32((date1 - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds);
    int dateStamp2 = Convert.ToInt32((date2 - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds);

    var eventDate = from DataModel.TB_EVENT_LOG el in SessionContext.DBContext.TB_EVENT_LOG
                   where el.nDateTime >= dateStamp1 && el.nDateTime <= dateStamp2 && el.nUserID == idUser
                   select el.nDateTime;

    List<Object[]> dates = new List<Object[]>();
    List<String> datet = new List<String>();

    foreach (var date in eventDate)
    {
        List<String> times = new List<String>();
        System.DateTime result = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
        String dateShort = result.AddSeconds((double)date).ToShortDateString();
        if (!datet.Contains(dateShort))
        {
            datet.Add(dateShort);                   

            int dateStamp3 = Convert.ToInt32((DateTime.Parse(dateShort) - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds);
            int dateStamp4 = Convert.ToInt32((DateTime.Parse(dateShort).AddDays(1) - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds);

            var eventTimes = from DataModel.TB_EVENT_LOG el in SessionContext.DBContext.TB_EVENT_LOG
                             where el.nDateTime >= dateStamp3 && el.nDateTime < dateStamp4 && el.nUserID == idUser
                             select el.nDateTime;

            String stringtimes = "";

            foreach (var datetime in eventTimes)
            {
                DateTime timeShort = result.AddSeconds((double)datetime).ToLocalTime();
                times.Add(timeShort.TimeOfDay.ToString("hh\\:mm\\ "));
            }

            stringtimes = string.Join("- ", times);

            dates.Add(new Object[]{
            dateShort.ToString(),stringtimes.ToString()
        });
        }
    }
    return dates;
}

你有什么建议

1 个答案:

答案 0 :(得分:1)

假设每个Object [](换句话说每行)由两个strings组成,那么这就是你需要的:

    <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding YourList}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Date" Binding="{Binding [0]}" />
            <DataGridTextColumn Header="Time" Binding="{Binding [1]}" />
        </DataGrid.Columns>
    </DataGrid>