I m using XAML written below
<Window x:Class="ERP.WinApp.Views.Admin.Patients"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Patients" Height="auto" MinWidth="1024" Width="1024" ShowInTaskbar="False" Icon="/ERP.WinApp;component/Images/patient.png" WindowStartupLocation="CenterScreen">
<Grid>
<DataGrid ItemsSource="{Binding}" Grid.Row="1" Name="gridPatients" ></DataGrid>
</Grid>
</Window>
And the Code behind written below
namespace ERP.WinApp.Views.Admin
{
/// <summary>
/// Interaction logic for Patients.xaml
/// </summary>
public partial class Patients : Window
{
public Patients()
{
InitializeComponent();
List<Patient> list = new List<Patient>();
list = // Populate it through some method
gridPatients.DataContext = list;
}
}
}
Patient class is having few simple properties
public class Patient
{
public int Id { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string FullName { get{ return this.FirstName+ " " +this.MiddleName+ " " +this.LastName; } }
public DateTime DOB { get; set; }
public int Age { get { return DateTime.Today.Year - this.DOB.Year; } }
public char Gender { get; set; }
}
When I run the app my datagrid is having all columns with data in it while if i want to have few columns only like skipping Id and age and first,middle,last name then what is the best way.
I think doing this is bad way for each column i want to hide
gridPatients.Columns[0].Visibility = Visibility.Collapsed;
You can set
AutoGenerateColumnstofalse, and add required columns.