0
votes

I'm writing an import utility, I'd like for users to be able to upload from a CSV and then direct the columns where they'd like them to go.

So in order to do this, I'm making the header of each column a ComboBox populated with all of the possible columns.

xaml

        <DataGrid x:Name="ImportTable" 
                  ItemsSource="{Binding displayTable}"
                  AutoGeneratingColumn="OnAutoGeneratingColumn"
                  AutoGenerateColumns="True"
                  CanUserAddRows="True" 
                  CanUserDeleteRows="True"
                  EnableColumnVirtualization="True"
                  EnableRowVirtualization="True"
                  MaxWidth="1300"
                  MaxHeight="600"
                  />

xaml.cs

    private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {

        var cb = new ComboBox();
        foreach (DataColumn test in (DataContext as EnterValueDialogViewModel).displayTable.Columns)
            Console.Out.WriteLine(test);

        cb.ItemsSource = (DataContext as EnterValueDialogViewModel).displayTable.Columns;
        e.Column.Header = cb;
    }

This correctly prints out all of the columns, but nothing is actually displayed inside of the combo box's

Empty Combobox


The combobox now displays properly in the dropdown. but i can't get it's selectedValue to set. The following code prints out that the selectedvalue is correct, but it's still populating initially as blank/unselected

        var cb = new ComboBox();
        cb.DisplayMemberPath = "ColumnName";
        cb.SelectedValue = e.PropertyName.ToString();
        cb.ItemsSource = (DataContext as EnterValueDialogViewModel).displayTable.Columns;
        Console.Out.WriteLine(cb.SelectedValue);
        e.Column.Header = cb;
2
Have you tried setting the DisplayMemberPath property for your combobox? To me it looks like you may have items, those items are just displayed as blanks.Sudsy1002
how would you go about setting this?The man holds me down at 1 Rep

2 Answers

1
votes

You can try to set the DisplayMemberPath of your combobox.

    private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {

        var cb = new ComboBox();
        cb.DisplayMemberPath = "SomePropertyFromYourCollection";
        foreach (DataColumn test in (DataContext as EnterValueDialogViewModel).displayTable.Columns)
            Console.Out.WriteLine(test);

        cb.ItemsSource = (DataContext as EnterValueDialogViewModel).displayTable.Columns;
        e.Column.Header = cb;
    }

Documentation here.

0
votes

You need to set the DisplayMemberPath and the SelectedValuePath of the combo box I think.

These will be column names from your data file.

It should look something like this:

private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    var cb = new ComboBox();
    foreach (DataColumn test in (DataContext as EnterValueDialogViewModel).displayTable.Columns)
        Console.Out.WriteLine(test);

    cb.ItemsSource = (DataContext as EnterValueDialogViewModel).displayTable.Columns;
    cb.DisplayMemberPath = "Column1";
    cb.SelectedValuePath = "Column2";
    e.Column.Header = cb;
}   

Let me know if it helped.