DataGridComboBoxColumn binding to DataGrid DataContext

1k views Asked by At

I'm about to give up. I've been trying to bind this DataGridComboBoxColumn to the DataGrid's DataContext (a DataSet) but I just can't. The code looks like this:

UserControl C#:

private static String strDBPath = EMS.Properties.Settings.Default.DBFile;
private static OleDbConnection myConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strDBPath + ";Jet OLEDB:Engine Type=5");
private static string strProp = "SELECT * FROM tblProperties";
private static OleDbDataAdapter adapterProp = new OleDbDataAdapter(strProp, myConn);
private static DataSet dsProp = new DataSet();
public ObservableCollection<string> VarTypes { get; set; }

public UserControlPropertiesAccess() {
     VarTypes = new ObservableCollection<string>() { "N", "Bool", "A2", "A4", "A8", "A20", "A50", "A100", "A200", };

     InitializeComponent();
     DatabaseHandling.CreateTable("tblProperties");
     adapterProp.Fill(dsProp, "LoadDataBindingProp");
     dgProperties.DataContext = dsProp;
}

In dsProp I have the following fields: PropID, PropName, PropVarType and PropUnit. I use binding to display the last three fields in other columns in my DataGrid and it all works fine. When I try to use DataGridComboBoxColumn it just does not bind to the DataContext. I only managed to have my ObservableCollection as my combobox items.

XAML:

    <DataGrid Name="dgProperties" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="LightCyan" Style="{StaticResource AzureDataGrid}" AutoGenerateColumns="False" CanUserAddRows="False" SelectionMode="Single" ItemsSource="{Binding Path=LoadDataBindingProp}" IsReadOnly="True" SelectionChanged="dgProperties_SelectionChanged">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=PropName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Property" Width="150" />
            <DataGridComboBoxColumn x:Name="dgcmbProperties" SelectedItemBinding="{Binding Path=PropName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Variable" Width="80" />
             <DataGridTextColumn Binding="{Binding Path=PropUnit, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Unit" Width="100" />
        </DataGrid.Columns>
    </DataGrid>

After the user finishes editing, I upload the data into my database using:

    private void btnSaveProperty_Click(object sender, RoutedEventArgs e)
    {
        myConn.Open();
        adapterProp.Update(dsProp.Tables[0]);
        myConn.Close();
    }

So I all want is bind my DataGridComboBoxColumn to my DataSet. Also, the selected item is not showing in the DataGrid cell.

Any help will be very much appreciated.

1

There are 1 answers

0
Bizhan On

You didn't set ItemsSource for the DataGridComboBoxColumn

I suppose your main view model (DataContext of main window) has a collection containing all combo box items (like MyComboItems)

Then this should work:

<Window x:Name="root">
    <DataGrid ItemsSource="{Binding Path=LoadDataBindingProp}">
        <DataGrid.Columns>
            <DataGridComboBoxColumn x:Name="dgcmbProperties" Header="Variable"
                  ItemsSource="{Binding 
                      ElementName=root, 
                      Path=DataContext.MyComboItems}"
                  SelectedItemBinding="{Binding 
                      Path=PropName, 
                      Mode=TwoWay, 
                      UpdateSourceTrigger=PropertyChanged}"/>
        </DataGrid.Columns>
    </DataGrid>
</Window>