Hello, I decided to learn about the proper placement and reading in and out of the datagrid in WFP. I faced a very difficult situation and decided it very badly, in my opinion.

1) I have a connected database, all field names in English. language, and in the submission on datagrid it is necessary that the field names are in Russian.

2) I decided to use var , for example var a = from b in db.clients select new { Имя = b.Name };

3) I immediately remembered that I wouldn’t have fun with var, so I made a method that would make a string from item 2 and set datagrid.itemssource = a ;

4) well, when reading, I ran into an even bigger problem ... I can not read the selected line in the datagridrow class, it tries to get into an anonymous type ... But if I make the var variable, I get the object output.

5) in the end, I use this: dynamic changerow = dataGrid.SelectedValue; during the execution of errors is not caused, but in my opinion it is terrible. I ask you to help me understand how I can correctly bind the data so that I can read and write data in a datagrid without any problems if it is possible without binding in xml . Thank!

  • Create a class, create instead of anonymous classes - the type of classes known to you and in selected will be the same. - Monk
  • Good option, thanks. I think further only if I get attached via xml, I don’t see a more profitable way - simply good

1 answer 1

1.DataGrid. Everything can be done much easier. However, it is necessary to describe each column manually. In principle, you can make a binding, as you did in point 3 datagrid.ItemSource = a; It will be more correct to make all the same in the XAML code. a must be public. The type of the column may be different. This page will help you.

 <DataGrid AutoGenerateColumns="False" ItemSource = {Binding a} SelectedItem ={Binding selectedItem}> <DataGrid.Columns > <DataGridTextColumn x:Name="Name" Header="Ваш текст сюда" Binding="{Binding Path=Name}" HeaderStyle=""></DataGridTextColumn> 

  1. Correct data binding. I advise you to ask what is ORM, for example, EF, and what is the MVVM approach and how everything works in conjunction
  • Yes, I am aware of ORM and EF, there was a little bit different task, which did not require the creation of a whole database. thanks - simply good