public void RefreshGui() { salRepo = new SalaryRepository(); //contains data salRepo.GetAll(); if (empRepo != null && salRepo != null) { List <SoloSalary> salaries = salRepo.GetAll().ToList(); var employees = empRepo.GetAll(); int selectedRowComfortGui; //for user comfort try { selectedRowComfortGui = salDataGridView.CurrentCell.RowIndex; } catch { selectedRowComfortGui = 0; } salDataGridView.ClearSelection(); //cleaning previos search salDataGridView.Columns.Clear(); //cleaning previous content salDataGridView.Rows.Clear(); //cleaning previous content //columns headers DataGridViewColumn d1 = new DataGridViewTextBoxColumn(); d1.HeaderText = "ID"; d1.Visible = false; d1.SortMode = DataGridViewColumnSortMode.Automatic; DataGridViewColumn d2 = new DataGridViewTextBoxColumn(); d2.HeaderText = "Employee"; d2.SortMode = DataGridViewColumnSortMode.Automatic; DataGridViewColumn d3 = new DataGridViewTextBoxColumn(); d3.HeaderText = "Amount €"; d3.SortMode = DataGridViewColumnSortMode.Automatic; DataGridViewColumn d4 = new DataGridViewTextBoxColumn(); d4.HeaderText = "Earn from"; d4.SortMode = DataGridViewColumnSortMode.Automatic; DataGridViewColumn d5 = new DataGridViewTextBoxColumn(); d5.HeaderText = "Earn to"; d5.SortMode = DataGridViewColumnSortMode.Automatic; salDataGridView.Columns.AddRange(d1, d2, d3, d4, d5); foreach (SoloSalary sal in salaries) { var row = new DataGridViewRow(); row.Cells.Add(new DataGridViewTextBoxCell { Value = sal.IDsal }); SoloEmployer se = empRepo.GetByID(sal.IDemp); row.Cells.Add(new DataGridViewTextBoxCell { Value = se.Name1 + " " + se.Name2 + " " + se.Name3 }); row.Cells.Add(new DataGridViewTextBoxCell { Value = sal.Amount }); row.Cells.Add(new DataGridViewTextBoxCell { Value = sal.validFrom.ToString(dateTimeFormat) }); row.Cells.Add(new DataGridViewTextBoxCell { Value = sal.validUntil.ToString(dateTimeFormat) }); salDataGridView.Rows.Add(row); //finalize row } salDataGridView.Rows[selectedRowComfortGui].Selected = true; } }