Advertisement
sylviapsh

Employees with dataTable

Feb 1st, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4.  
  5. class Employees
  6. {
  7.  
  8.   static void Table(string[,] array)
  9.   {
  10.     DataTable dt = new DataTable();
  11.     // first row contains column names:
  12.     dt.Columns.Add("First Name");
  13.     dt.Columns.Add("Family Name");
  14.     dt.Columns.Add("Position");
  15.     dt.Columns["Position"].DataType = typeof(int);
  16.  
  17.     // load data from string array to data table:
  18.     for (int rowindex = 0; rowindex < array.GetLength(0); rowindex++)
  19.     {
  20.       DataRow row = dt.NewRow();
  21.       for (int col = 0; col < array.GetLength(1); col++)
  22.       {
  23.         row[col] = array[rowindex, col];
  24.       }
  25.       dt.Rows.Add(row);
  26.     }
  27.  
  28.     var viewDT = new DataView(dt);
  29.     viewDT.Sort = "Position DESC, Family Name ASC, First Name ASC";
  30.  
  31.     foreach (DataRowView dr in viewDT)
  32.     {
  33.       Console.WriteLine("{0} {1}", dr[0], dr[1]);
  34.     }
  35.   }
  36.  
  37.   static void Main()
  38.   {
  39.  
  40.     //Read position names and weights in a dictionary
  41.     int numOfPositions = int.Parse(Console.ReadLine());
  42.     Dictionary<string, string> positionTitles = new Dictionary<string, string>();
  43.  
  44.     for (int i = 0; i < numOfPositions; i++)
  45.     {
  46.  
  47.       string input = Console.ReadLine();
  48.       string[] temp = input.Split('-');
  49.       if (!positionTitles.ContainsKey(temp[0].Trim()))
  50.       {
  51.         positionTitles.Add(temp[0].Trim(), temp[1].Trim());
  52.       }
  53.     }
  54.  
  55.     //Read the employee names
  56.     int numOfEmployees = int.Parse(Console.ReadLine());
  57.     string[,] employees = new string[numOfEmployees, 3];
  58.     for (int i = 0; i < numOfEmployees; i++)
  59.     {
  60.       string input = Console.ReadLine();
  61.       string[] temp = input.Split('-');
  62.       string[] temp2 = temp[0].Split(' ');
  63.       employees[i, 0] = temp2[0].Trim();//First name
  64.       employees[i, 1] = temp2[1].Trim();//Family name
  65.       employees[i, 2] = temp[1].Trim();//Position name
  66.       string weight = "0";
  67.       positionTitles.TryGetValue(employees[i, 2], out weight);//Match with the dictionary of Positions and exchange the values to get the number
  68.       employees[i, 2] = weight;
  69.     }
  70.  
  71.     Table(employees);
  72.   }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement