gpt4 book ai didi

c# - 如何使用 List 在 WPF 中填充 DataGrid?

转载 作者:太空宇宙 更新时间:2023-11-03 13:07:21 24 4
gpt4 key购买 nike

我有这个填充列表的方法,它准备填充 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;
}

有什么建议

最佳答案

假设每个 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>

关于c# - 如何使用 List<Object[]> 在 WPF 中填充 DataGrid?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30314138/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com