0 Replies - 1593 Views - Last Post: 21 April 2014 - 01:27 PM

#1 Spectre5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-April 14

Show ms access data in wpf DataGridComboBoxColumn?

Posted 21 April 2014 - 01:27 PM

I established a connection to a MS Access database in my C# application and created a DataGrid by dragging the "tblPlates" table from the data sources window over to Mainwindow.cs. All the data in every field is showing up properly except the "plate_Size" field in comboboxes. I put them in a DataGridComboBoxColumn but there is no Binding option like there is in the text columns, so I tried Itemsource like the example below, as well SelectedValuePath, SelecetedValueBinding, and SelectedItemBinding. MSDN suggested adding a SelectedValueBinding after the Itemsource.

<DataGridComboBoxColumn x:Name="plate_SizeColumn" ItemsSource="{Binding plate_Size, Mode=TwoWay}" SelectedValueBinding="{Binding plate_Size}" Header="Plate Size" Width="SizeToHeader"/>

The "plate_Size" field was a text data type in the database with RowSource containing the values that the user could chose from.

I also tried moving the "plate_Size" data that needs to be in the combobox to a separate table (tblComboSize) so I could run a query and load the values into it one by one, but I'm not sure how to do it for a combobox that is already in a datagrid. There is no Items property or Add() method. The code below is how I would do it as if it was a standalone combobox.

void fillComboSizes(object sender, EventArgs e)
            OleDbConnection dbConnection = new OleDbConnection(connectionString);
            string fillSize = "SELECT * FROM tblComboSize";
            OleDbCommand checkUsername = new OleDbCommand(fillSize, dbConnection);
            OleDbDataReader dataReader = checkUsername.ExecuteReader();
            while (dataReader.Read())
                string size = dataReader.GetString(0);

Lastly, I tried binding the DataGridComboBoxColumn to the "tblComboSize" table directly. I added it as a source like the example below, and bound it to "tblComboSize" in the itemsource instead.

<CollectionViewSource x:Key="tblComboSizeViewSource" Source="{Binding tblComboSize, Source={StaticResource hAN464ProjectDataSet}}"/>

Basically, I just need the values that are in a combobox in access to appear in a combobox in wpf and also show up in a separate combobox not in the datagrid whenever that record is chosen. I got that part working with the textboxes like so:

<TextBox Text="{Binding ElementName=tblPlatesDataGrid, Path=SelectedItem.plate_Barcode}" HorizontalAlignment="Left" Height="23" Margin="145,25,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200"/>

... so I was wondering if this would work for the comboboxes if I did this:

<ComboBox SelectedItem="{Binding ElementName=tblPlatesDataGrid, Path=SelectedItem.plate_Size}" HorizontalAlignment="Left" Height="23" Margin="145,60,0,0" VerticalAlignment="Top" Width="200"/>>

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1