简体   繁体   English

对 wpf DataGrid 中的 DataTable 中的自动生成的列进行着色

[英]Colorize Autogenerated Columns from DataTable in wpf DataGrid

I am currently creating a DataTable with (optional) column names which I load into a DataGrid我目前正在创建一个带有(可选)列名的 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();
}

This is for previewing csv data, selecting the correct splitter, encoding and specifying column names.这是用于预览 csv 数据、选择正确的拆分器、编码和指定列名。

The Preview so far looks fine.到目前为止的预览看起来不错。 The headers are there and the data is there.标题在那里,数据在那里。 在此处输入图像描述

There might be a lot of e-waste-columns which are not beneficiary to my project or the Columns have inconsistent names, dependend which bank statement I'm importing.可能有很多电子废物列对我的项目没有好处,或者列的名称不一致,这取决于我要导入的银行对账单。 Hence I want to mark the Columns which are imported.因此我想标记导入的列。 I tried the following:我尝试了以下方法:

// 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)));

This works when I have added columns manually.这在我手动添加列时有效。 The Autogenerated Columns are not beeing considered:不考虑自动生成的列:

// 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'

在此处输入图像描述

please help colorizing autogeneraded Columns.请帮助为自动生成的列着色。

The Columns Get mapped correctly when adding Columns to the DataTable AND to the DataGrid:将列添加到 DataTable 和 DataGrid 时,列得到正确映射:

// generate Columns (with headers if selected)
for (int i = 0; i < ImportTable[0].Length; i++)
{
    csvPreview.Columns.Add();
    DataGridTextColumn col = new DataGridTextColumn();
    string columnName;
    if ((bool)HasHeaders_Checkbox.IsChecked)
    {
        // specify Column Name
        columnName = ImportTable[0][i];
    }
    else
    {
        // generate generic names
        columnName = "Column " + i;
    }
    csvPreview.Columns[i].ColumnName = columnName;
    col.Binding = new Binding(columnName);
    col.Header = columnName;
    this.ImportPreview.Columns.Add(col);
}

Result:结果: 在此处输入图像描述

Note: The generation of generic names is very important to map the columns.注意:通用名称的生成对 map 列非常重要。 If no generic names are specified, the mapping is incorrect and the result will look something like this:如果未指定通用名称,则映射不正确,结果将如下所示: 在此处输入图像描述

Note 2: AutoGenerateColumns="False" should be set in xaml to the Data grid注 2: AutoGenerateColumns="False"应在 xaml 中设置到数据网格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM