Question

I am using WPF. I am trying to use a grid to show table within my database. The goal is to have all the data in that grid. Example:

ID Firstname Lastname
1 John Smith
2 Jane Smith
However, each cell should be a combobox that has every choice for that particular column if clicked. So clicking John will display combo box with every firstname in the table, in this case it would say John and Jane. If the user chose to click on ID , it would display 1 and 2 and so forth.

What i have tried so far is to use a data table as the datagrids item source. This works perfectly, but I cannot add a combobox to a datatable. I can add a combobox column to the datagrid, but then I am no longer using the data table and am not sure how to iterate through each row in the database with using a comboboxcolumn.

So what I want is a combobox in each of the cells which shows the corresponding data for that particular row but upon clicking it will list all choices. I have searched around but I am not sure I am searching for the right things.

I have tried a few things here and there with the combo boxes but nothing worth noting. Also, I have auto generated columns, not sure if you can have non auto generated columns and still use a binding or how to define it, though.

This is the data table generating.

public DataTable PersonData()
{    
    List<Person> str4 = new List<Person>();
    DataTable _PersonData;

    _PersonData = new DataTable();
    _PersonData.Columns.Add(new DataColumn("FirstName", typeof(string)));
    _PersonData.Columns.Add(new DataColumn("LastName", typeof(string)));

    str4 = newquery();
    str4.ForEach(delegate(Person person1)
    {
         row3 = _PersonData.NewRow();
         _PersonData.Rows.Add(row3);
         row3["FirstName"] = person1.FirstName;
         row3["Lastname"] = person1.Lastname;
    });

    return _PersonData; 
 }

This ran when a user clicks on an item in a list box, it binds the data table.

private void youclickedon(String result)
{
     newdatatable = PersonData();
    Binding binding = new Binding() {Mode=BindingMode.OneWay, Source = newdatatable, Path = new PropertyPath(".") };
    BindingOperations.SetBinding(GridData, DataGrid.ItemsSourceProperty, binding);
    GridData.Columns[0].IsReadOnly = true;
    newdatatable.AcceptChanges();
}
Was it helpful?

Solution

I would create my data object behind myDataGrid with the following properties

  • ObservableCollection<MyObject> Records
  • List<int> Ids
  • List<string> FirstNames
  • List<string> LastNames

Then bind my DataGrid using TemplateColumns that have ComboBoxes which bind to the collections of values in the DataContext, like this:

<DataGrid ItemsSource="{Binding Records}">
    <DataGrid.Columns>
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.Ids}"
                              SelectedItem="{Binding Id}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.FirstNames}"
                              SelectedItem="{Binding FirstName}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.LastNames}"
                              SelectedItem="{Binding LastName}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

And I'd populate my lists at the time the grid loads (and maybe update it as items change if needed)

Ids = Records.Select(p => p.Id).ToList();
FirstNames = Records.Select(p => p.FirstName).ToList();
LastNames = Records.Select(p => p.LastName).ToList();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top