简体   繁体   中英

Colorize Autogenerated Columns from DataTable in wpf DataGrid

I am currently creating a DataTable with (optional) column names which I load into a 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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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