【发布时间】:2022-01-04 18:12:42
【问题描述】:
我目前正在创建一个带有(可选)列名的 DataTable,我将其加载到 DataGrid 中
List<string[]> ImportTable;
public void GeneratePreview()
{
// Build datatable
DataTable csvPreview = new DataTable();
// generate Columns (with headers if selected)
for (int i = 0; i < ImportTable[0].Length; i++)
{
csvPreview.Columns.Add();
if ((bool)HasHeaders_Checkbox.IsChecked)
{
// specify table headers
csvPreview.Columns[i].ColumnName = ImportTable[0][i];
}
}
/* Add records excluded here as not beneficiary for the question */
// build display output
this.ImportPreview.ItemsSource = csvPreview.AsDataView();
}
这用于预览 csv 数据、选择正确的拆分器、编码和指定列名。
可能有很多电子废物列对我的项目没有好处,或者列的名称不一致,这取决于我要导入的银行对帐单。 因此我想标记导入的列。 我尝试了以下方法:
// ImportPreview is my Datagrid in WPF xaml
this.ImportPreview.Columns[index].CellStyle = new Style(typeof(DataGridCell));
this.ImportPreview.Columns[index].CellStyle.Setters.Add(new Setter(DataGridCell.BackgroundProperty, new SolidColorBrush(Colors.LightBlue)));
这在我手动添加列时有效。不考虑自动生成的列:
// Index was 1 and the DataGrid actually has 8 Columns
System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index'
请帮助为自动生成的列着色。
【问题讨论】: