Patient Information Dashboard in WPF



        public MainWindow()
        {
            InitializeComponent();
        }
 
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //TODO: Get data from database
            //Bind to data grid.
            dgLoadData.ItemsSource = GetDataFromEmployeeList();
        }
 
        private List<Employee> GetDataFromEmployeeList()
        {
            List<Employee> employeeList = new List<Employee>();
            employeeList.Add(new Employee() { Age = 20, DateOfBirth = DateTime.Now.AddYears(-23), FirstName = "Girijesh", LastName = "Yadav" });
            employeeList.Add(new Employee() { Age = 30, DateOfBirth = DateTime.Now.AddYears(-13), FirstName = "Girijesh1", LastName = "Yadav1" });
            employeeList.Add(new Employee() { Age = 40, DateOfBirth = DateTime.Now.AddYears(-33), FirstName = "Girijesh2", LastName = "Yadav2" });
            employeeList.Add(new Employee() { Age = 50, DateOfBirth = DateTime.Now.AddYears(-53), FirstName = "Girijesh3", LastName = "Yadav3" });
            employeeList.Add(new Employee() { Age = 60, DateOfBirth = DateTime.Now.AddYears(-73), FirstName = "Girijesh4", LastName = "Yadav4" });
            employeeList.Add(new Employee() { Age = 70, DateOfBirth = DateTime.Now.AddYears(3), FirstName = "Girijesh6", LastName = "Yadav6" });
            return employeeList;
        }
 
        private void ExporttoCSV_Click(object sender, RoutedEventArgs e)
        {
            bool flag = ToCSV(ToDataTable<Employee>(GetDataFromEmployeeList()), "C:\\" + DateTime.Now.Ticks + ".csv");
            if (flag)
            {
                MessageBox.Show("File exported to CSV successfully.");
            }
            else
            {
                MessageBox.Show("Please try later.");
            }
        }

       public  DataTable ToDataTable<T>(IEnumerable<T> items)
       {
           var tb = new DataTable(typeof(T).Name);
 
           PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
 
           foreach (var prop in props)
           {
               tb.Columns.Add(prop.Name, prop.PropertyType);
           }
 
           foreach (var item in items)
           {
               var values = new object[props.Length];
               for (var i = 0; i < props.Length; i++)
               {
                   values[i] = props[i].GetValue(item, null);
               }
 
               tb.Rows.Add(values);
           }
 
           return tb;
       }


      public bool ToCSV(DataTable dtDataTable, string strFilePath)
      {
          bool result;
          try
          {
              StreamWriter sw = new StreamWriter(strFilePath, false);
              //headers  
              for (int i = 0; i < dtDataTable.Columns.Count; i++)
              {
                  sw.Write(dtDataTable.Columns[i]);
                  if (i < dtDataTable.Columns.Count - 1)
                  {
                      sw.Write(",");
                  }
              }
              sw.Write(sw.NewLine);
              foreach (DataRow dr in dtDataTable.Rows)
              {
                  for (int i = 0; i < dtDataTable.Columns.Count; i++)
                  {
                      if (!Convert.IsDBNull(dr[i]))
                      {
                          string value = dr[i].ToString();
                          if (value.Contains(','))
                          {
                              value = String.Format("\"{0}\"", value);
                              sw.Write(value);
                          }
                          else
                          {
                              sw.Write(dr[i].ToString());
                          }
                      }
                      if (i < dtDataTable.Columns.Count - 1)
                      {
                          sw.Write(",");
                      }
                  }
                  sw.Write(sw.NewLine);
              }
              sw.Close();
              result = true;
 
          }
          catch (Exception)
          {
              result = false;
          }
          return result;
      }

====================================================================================
    class Employee
    {
        private int age;
 
        public int Age
        {
            get { return age; }
            set { age = value; }
        }
        public string FirstName { getset; }
        public string LastName { getset; }
        public DateTime DateOfBirth { getset; }
    }
---------------------------------------------------------------
<Window x:Class="PatientInformationDashboard.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="623.684" Width="930.263">
    <Grid Margin="0,0,2,0">
        <DatePicker HorizontalAlignment="Left" Margin="133,121,0,0" VerticalAlignment="Top"/>
        <DatePicker HorizontalAlignment="Left" Margin="541,124,0,0" VerticalAlignment="Top"/>
        <StackPanel Margin="224,33,60,493" Orientation="Vertical">
            <Label Content="Patient Info Dashboard" HorizontalAlignment="Stretch" VerticalAlignment="Top" FontSize="40" FontFamily="Stencil" FontWeight="Bold" Height="58" Margin="26,0,10,0"/>
        </StackPanel>
        <Label Content="From :" HorizontalAlignment="Left" Margin="39,121,0,0" VerticalAlignment="Top" Width="70" FontSize="16" FontWeight="Bold"/>
        <Label Content="To :" HorizontalAlignment="Left" Margin="489,121,0,0" VerticalAlignment="Top" Width="70" FontWeight="Bold" FontSize="14"/>
        <DataGrid x:Name="dgLoadData" HorizontalAlignment="Left" Margin="39,177,0,0" VerticalAlignment="Top" Height="385" Width="854" ItemsSource="{Binding Source=Employee}"  AutoGenerateColumns="False" CanUserResizeColumns="True">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Age" Binding="{Binding Age}"/>
                <DataGridTextColumn Header="Registration Date" Binding="{Binding DateOfBirth}"/>
                <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"/>
                <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"/>
            </DataGrid.Columns>
        </DataGrid>
        <Button Content="Fetch Data" HorizontalAlignment="Left" Margin="771,125,0,0" VerticalAlignment="Top" Width="122" FontWeight="Bold" FontSize="16" Click="Button_Click"/>
        <Button Content="Export to CSV" HorizontalAlignment="Left" Margin="771,93,0,0" VerticalAlignment="Top" Width="122" FontWeight="Bold" FontSize="16" Click="ExporttoCSV_Click"/>
 
    </Grid>
</Window>

Comments

Popular posts from this blog

25ThApril 2020