Reputation: 121
I have a program in WPF where I load a bunch of data in a Datagrid.
Datagrid xaml:
<Window x:Class="test.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:test"
mc:Ignorable="d"
Title="Window1" Height="450" Width="800">
<Grid>
<DataGrid x:Name="dataGrid" Margin="10" Loaded="DataGrid_Loaded" ClipToBounds="True" AutoGenerateColumns="True" SelectionChanged="DataGrid_SelectionChanged_2" >
<DataGrid.Columns>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
Then I add a Combobox column to the Datagrid
Code:
DataTable table = new DataTable();
DataGridComboBoxColumn dgvCmb = new DataGridComboBoxColumn();
dgvCmb.ItemsSource = new List<string>
{
"Ghanashyam",
"Jignesh",
"Ishver",
"Anand"
};
dataGrid.Columns.Add(dgvCmb);
The values are temporary for testing and will be changed later on.
The idea is that the user selects something in the Combobox depending on the other data in that row (my current test set is 810 rows long ).
The issue I came across is, whenever I select something in the Combobox and change row the selected data is removed from the previous row.
So naturally, I thought I need to handle this myself. After a little searching on the internet I found this:
But when I tried to implement this I found out my Datagrid doesn't recognize that event.
I checked and apparently, there are two namespaces that contain a Datagrid object:
system.windows.forms.datagrid
and
system.windows.controls.datagrid
Am I using the wrong one? Because in the toolbox in Visual Studio I only see one Datagrid.
If this situation can be handled better any advice will be appreciated.
Upvotes: 0
Views: 81
Reputation: 169400
You should store the selected value in the ComboBox
in a column of your DataTable
using a SelectedItemBinding
:
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("SelectedValue"));
DataGridComboBoxColumn dgvCmb = new DataGridComboBoxColumn();
dgvCmb.SelectedItemBinding = new Binding("SelectedValue");
dgvCmb.ItemsSource = new List<string>
{
"Ghanashyam",
"Jignesh",
"Ishver",
"Anand"
};
dataGrid.Columns.Add(dgvCmb);
Upvotes: 1