[RESOLVIDO] Formatação DataGridView

Tenho uma DataGridView que recebe um DataSource, como o exemplo abaixo. Após a carga do DataSource, faço a formatação dela, cores, fontes e etc.

Pergunto:

Há alguma forma mais inteligente de fazer isso?

Pois quando faço dessa forma, a Grid carrega toda sem formatação e depois ela recebe a formatação, gerando dois refresh como uma onda em tela.

private void CarregaDados()
{
	SqlDataReader leitor;
	SqlCommand cmd;

	using (IDbConnection conexao = ConnectionFactory.CriaConexao())
	using (IDbCommand comando = conexao.CreateCommand())
	{

		cmd = (SqlCommand)comando;
		cmd.CommandText = query + order;
		//comando.CommandText = query;
		leitor = cmd.ExecuteReader();// comando.ExecuteReader();
		if (!leitor.HasRows)
		{
			MessageBox.Show("Não há Pallets neste status", "Aviso", MessageBoxButtons.OK);
			return;
		}		
		binding.DataSource = leitor;
		dgMonitor.DataSource = binding;
	}	

}

private void formatagrid2(object sender, DataGridViewCellFormattingEventArgs e)
{

	if (dgMonitor.Rows[e.RowIndex].Cells["Status"].Value.ToString().Trim() == "DEVOLVIDO")
	{                
		dgMonitor.Rows[e.RowIndex].Cells["Status"].Style.BackColor = Color.OrangeRed;
	}                  
					   
	if (dgMonitor.Rows[e.RowIndex].Cells["Status"].Value.ToString().Trim() == "EXPEDICAO")
	{                  
		dgMonitor.Rows[e.RowIndex].Cells["Status"].Style.BackColor = Color.DeepSkyBlue;
	}                  
					   
	if (dgMonitor.Rows[e.RowIndex].Cells["Status"].Value.ToString().Trim() == "RECEBIDO")
	{                  
		dgMonitor.Rows[e.RowIndex].Cells["Status"].Style.BackColor = Color.GreenYellow;
	}                  
	if (dgMonitor.Rows[e.RowIndex].Cells["Status"].Value.ToString().Trim() == "TRANSITO")
	{                  
		dgMonitor.Rows[e.RowIndex].Cells["Status"].Style.BackColor = Color.Gold;
	}

	//Altura da linha
	dgMonitor.Rows[e.RowIndex].Height = 35;

	dgMonitor.Rows[e.RowIndex].Cells["Status"].Style.Font = oFontg;
	dgMonitor.Rows[e.RowIndex].Cells["Emissão"].Style.Font = oFontg;
	dgMonitor.Rows[e.RowIndex].Cells["Hora"].Style.Font = oFontg;
	dgMonitor.Rows[e.RowIndex].Cells["Sequência"].Style.Font = oFontg;
	dgMonitor.Rows[e.RowIndex].Cells["Produto"].Style.Font = oFontg;
	dgMonitor.Rows[e.RowIndex].Cells["OP"].Style.Font = oFontg;
	dgMonitor.Rows[e.RowIndex].Cells["Lote"].Style.Font = oFontg;
	//dgMonitor.Rows[e.RowIndex].Cells["Ficha Pallet"].Style.Font = oFontg;
	dgMonitor.Rows[e.RowIndex].Cells["Carga"].Style.Font = oFontg;
	dgMonitor.Rows[e.RowIndex].Cells["Nota Fiscal"].Style.Font = oFontg;
	dgMonitor.Rows[e.RowIndex].Cells["Quantidade Lida"].Style.Font = oFontg;
	dgMonitor.Rows[e.RowIndex].Cells["Quantidade Apontada"].Style.Font = oFontg;
}

Configure o DataGridView com a formatação prévia em vez de codificar isso, realmente sua preocupação é válida.

Então, configure-o e assim só passe o valor para que o DataSource faça o carregamento

1 curtida

Fiz um teste como você sugeriu, e ficou muito bom. Melhorou até a performance.

1 curtida