我正在从数据库加载DataTable,但不希望它看起来像它。这就是为什么我决定创建一个新的DataTable并用我收到的数据填充它。 不幸的是,数据没有完全显示在DataGrid中。更详细地说,只显示了第一列,但没有显示所有其他列。在调试时,我可以确保正确填充DataTable。
你们有谁知道我做错了什么?
型号:
internal DataTable getValueTable(ObservableCollection<CheckableMenuitem> listSelectableValues)
{
DataTable TableToReturn = new DataTable();
string LaborValueColumnCaption = Properties.Resources.LabValueColumnCaption;
TableToReturn.Columns.Add(LaborValueColumnCaption);
List<string> selectedValueNames = new List<string>();
foreach (CheckableMenuitem item in listSelectableValues.Where(x => x.IsChecked == true))
{
selectedValueNames.Add(item.Caption);
}
string selectionFormular = "Wertname in ('" + string.Join("', '", selectedValueNames) + "')";
DataRow[] selectionRows = fullValueTable.Select(selectionFormular);
HashSet<DateTime> ListOfDays = new HashSet<DateTime>();
foreach (DataRow item in selectionRows)
{
DateTime? Tmp = Functions.ParseNullableDate(item["Messdatum"].ToString());
if (Tmp != null)
{
ListOfDays.Add(((DateTime)Tmp).Date);
}
}
string DateFormatString = "dd.MM.yyyy";
foreach (DateTime date in ListOfDays)
{
TableToReturn.Columns.Add(date.ToString(DateFormatString));
}
foreach (var valueName in selectedValueNames)
{
string singleSelectionFormular = "Wertname ='" + valueName + "'";
List<DataRow> singleSelectionRows = fullValueTable.Select(singleSelectionFormular).ToList();
if (singleSelectionRows.Count() == 1)
{
DateTime? ValueDate = Functions.ParseNullableDate(singleSelectionRows[0]["Messdatum"].ToString());
if (ValueDate != null)
{
}
DataRow newRow = TableToReturn.NewRow();
//erste Spalte
newRow[LaborValueColumnCaption] = valueName;
string spalte = ((DateTime)ValueDate).ToString(DateFormatString);
newRow[spalte] = singleSelectionRows[0]["MESSWERT_ALPHA"].ToString();
//newRow[((DateTime)ValueDate).ToString(DateFormatString)] = singleSelectionRows[0]["MESSWERT_ALPHA"].ToString();
TableToReturn.Rows.Add(newRow);
}
if (singleSelectionRows.Count() > 1)
{
List<DataRow> Rows = new List<DataRow>() { TableToReturn.NewRow() };
Rows[0][LaborValueColumnCaption] = valueName;
HashSet<DateTime> usedDates = new HashSet<DateTime>();
foreach (var item in singleSelectionRows)
{
DateTime? ValueDate = Functions.ParseNullableDate(singleSelectionRows[0]["Messdatum"].ToString());
if (ValueDate != null)
{
DateTime Date = ((DateTime)ValueDate).Date;
if (usedDates.Add(Date))
{
string name = "Wertname ='" + valueName + "' and Messdatum > " + Date + " and Messdatum < " + Date.AddDays(1);
DataRow[] test2 = fullValueTable.Select(singleSelectionFormular);
//Rows[0][((DateTime)ValueDate).ToString(DateFormatString)] = singleSelectionRows[0]["MESSWERT_ALPHA"].ToString();
}
}
}
foreach (DataRow newRow in Rows)
{
TableToReturn.Rows.Add(newRow);
}
}
}
return TableToReturn;
}
视图模型:
public DataTable ValueTable
{
get
{
return model.getValueTable(ListSelectableValues);
}
}
查看:
<DataGrid DockPanel.Dock="Top" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" AutoGenerateColumns="True" ItemsSource="{Binding Path=ValueTable}"/>