Exemplo n.º 1
0
        private void CarregaUltimasPlacas()
        {
            try
            {
                List <RegistrosModel> list = SqliteDataAccess.CarregaUltimasPlacas();

                var columns = from t in list
                              orderby t.data_entrada descending
                              select new
                {
                    id      = t.id,
                    Placa   = t.placa,
                    Entrada = t.data_entrada,
                    Saída   = t.data_saida/*,
                                           * Diárias = (t.data_saida == DateTime.MinValue ? 0 : (t.data_saida - t.data_entrada).TotalDays),
                                           * Valor = RetornaValor(t.data_entrada, t.data_saida, t.tipo).ToString()*/
                };

                //Armazena a linha para seleciona-la após o refresh.
                if (dataGridView1.CurrentRow != null)
                {
                    linha = dataGridView1.CurrentRow.Index;
                }
                else
                {
                    linha = 0;
                }

                //dataGridView1.BeginEdit();
                dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma", 9, FontStyle.Bold);
                dataGridView1.DefaultCellStyle.Font = new Font("Tahoma", 9);
                dataGridView1.DataSource            = columns.ToList();

                if (columns.ToList().Count > 0)
                {
                    dataGridView1.AutoSizeColumnsMode     = DataGridViewAutoSizeColumnsMode.DisplayedCells;
                    dataGridView1.AutoSizeColumnsMode     = DataGridViewAutoSizeColumnsMode.DisplayedCells;
                    dataGridView1.Columns[0].Visible      = false;
                    dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                    dataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                    dataGridView1.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;

                    dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                    dataGridView1.MultiSelect   = false;

                    if (columns.ToList().Count > 1)
                    {
                        dataGridView1_CellClick(dataGridView1, new DataGridViewCellEventArgs(0, linha));
                    }

                    if (dataGridView1.CurrentRow != null)
                    {
                        dataGridView1.CurrentRow.Selected = true;
                    }
                    else
                    {
                        dataGridView1.Rows[linha].Selected = true;
                    }
                }

                dataGridView1.Refresh();
                dataGridView1.EndEdit();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }