WPF Datagrid with comboboxes and loading from DataSet
I'm absolutely new to WPF and trying to make a simple application with DataGrid. This application receives Employees DataSet from WebService and has to allow viewing and editing data. DataSet contains three tables: Employees (id, Name, GenderId, PositionId), Genders (id, Name) and Positions (Id, Name). Employees table connected with Genders via GenderId and with Positions via PositionId. I've dragged the Employees DataSet to the window and got the DataGrid which allows to edit items. But now it contains IDs of Position and Gender, and I need ComboBoxes which will allow to select Gender or Position from available values and save them back to the DataSet. I load the DataSet in the following way:
EmployeesProxy.EmployeesSe开发者_运维知识库rviceClient proxy = new EmployeesProxy.EmployeesServiceClient();
proxy.ClientCredentials.UserName.UserName = "user1";
proxy.ClientCredentials.UserName.Password = "pass1";
empDataSet = proxy.GetEmployeesData();
employeesDataGrid.ItemsSource = empDataSet.Employees.DefaultView;
GenderDT = empDataSet.Gender;
proxy.Close();
I have empDataSet and GenderDT as properties in my window. However, I could not create a column in XAML with comboboxes which will allow to edit data. Is there a way to do this?
EDIT: I found this solution which partially works (I can select Gender in edit mode, but cannot view Gender name in normal mode, I see only Id):
<DataGridTemplateColumn Header="Gender">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=GenderDT,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Window}}}"
DisplayMemberPath="Name"
SelectedValuePath="Gender" SelectionChanged="ComboBox_SelectionChanged" Loaded="ComboBox_Loaded"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=GenderId}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
After changing ComboBox I change the Gender Column:
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
((DataRowView)employeesDataGrid.SelectedItem).Row["GenderId"] = ((DataRowView)((ComboBox)sender).SelectedItem).Row["Id"];
}
But, as for me, this is not good practice. And i still don't get the desired result. I thought there might be a simple way to do such task.
I have the same problem and have found the solution. I hope this will help somebody. First you have to data bind the DataGrid to the master table (in your case, Employees):
myDataGrid.ItemsSource = dataset.Employees.DefaultView;
Then add a combo to the DataGrid and give it a name, for example: positionCombo. Then you have to data bind the combo (positionCombo) to the related table
positionCombo.ItemsSource = dataset.Positions.DefaultView;
After that you have to specify some properties in the XAML:
<DataGrid x:Name="myDataGrid" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridComboBoxColumn x:Name="positionCombo" Header="Position" DisplayMemberPath="Name" SelectedValuePath="Id" SelectedValueBinding="{Binding PositionId}"/>
</DataGrid.Columns>
</DataGrid>
Note that (Name, Id) are fields in your "Positions" table and "PositionId" is FK in your "Employees" table.
You can use a DataGridTemplateColumn with something like:
<DataGridTemplateColumn >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox >
....
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
精彩评论