c# - WPF DataGrid Parent Child Data -


I'm new to WPF / C # / NET but I'm learning a few short practices through coding.

Anyway, I'm stuck. I've sebaked here and on Google and I can not find the answer or I can not find any answer.

My problem is ... I have to use the unit framework two tables. One is for employee details and company details. Work for employees 0 or 1 company.

I want to define a datagrid for navigating the employee through WPF / XAML. But in every employee line, I would like to show the names of those companies which they work (if there is a relationship) or "unemployed" in those cases where there is no related company record.

I do not have the details of tables because it really does not matter - the problem is displaying information consolidated with the parent / child relationship in a single datagread.

I do not know what is the best way to deal with such a problem, while considering WPF / DataGrid, I really appreciate helping to do this, binding (assuming WPF) Or an example of WPF / XAML

thanks in advance.

There are several ways to accomplish this - in a way you can try to create a visual model To display the data that you want to encrypt - eg.

  Public Class Employee Visualdial {Private Redoni employee employee employee; Public Employee View Model (employee employee) {_employee = employee; } Public string name {get {return_employee.Name; }} Public string CompanyName {get {return_employee.Company == null? "Unemployed": _employee.Company.CompanyName; Then, a  IEnumerable & lt; Employee & gt; , you can project your employee data in this visual model and set it as the source of your datagrid as source - e.g. 

IEnumerable & lt; Employee & gt; Employees = GetEmployeesFromDatabase (); DataGrid1.ItemsSource = Employees. Selection (x = & amp;; New Staff View Model (x));

You will usually set the items instead of setting the XML directly through the XML binding, but in this the parent ViewModel set will be set as the Vide datacontext and I am trying to keep things simple.

Another way to accomplish this with a data grader is to leave the use of a visual model, directly to a IEnumerable & lt; Employee & gt; Collections and set column bindings explicitly - e.g.

  & lt; DataGrade ItemsSource = "{binding employee}" auto generated column = "false" & gt; & Lt; DataGrid.Columns & gt; & Lt; DataGridTextColumn header = "employee's name" binding = "{binding name}" /> & Lt; DataGridTextColumn header = "company name" binding = "{binding company. Name}" /> & Lt; /DataGrid.Columns> & Lt; / Data grid & gt;  

Note that with the second example, you will not see "unemployed" in the company name column, where there is no affiliate company for the employee.

EDIT: To clarify the point about the source of bound items to see your grid on a 'main' visual model, you can create a ViewModel class that represents the entire current view Does it

  Public Sector Main View Model {Private Readonly IEnumerable & lt; Employee & gt; _ Workers; Public Main Visual Model (IEnumerable & lt; Employee & gt; Employee) {_employees = Employee; } Public IEnumerable & lt; Employee & gt; Employee {receive (return worker); }}}  

You set it as the data content for window / user controls.

For example, in simple cases you can do this in the constructor for your window (though you should call the database, where there should probably be asynchronous for WPF applications).

  Public Menwindows () {InitializeComponent (); Datacontax = new main theme (getAllEmployees ()); }  

Then you can set binding on your grid like this:

  & lt; DataGrid ItemsSource = "{binding employee}" ...  

The data reference for the main window is then used for all its hair controls, out of which you can explicitly control the hair Data contact sets. This technique proves to be useful, where you have more complex view models with lots of assets and commands. If you are interested in learning more then you should take a look at the MVVM pattern and either both or the prism / caliber framework.


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -