I'm having a question in C # regarding DataGridView.
In this scenario I have a Form where I run a Datagrid bringing information from another class. After the data is loaded by the Datagrid.Datasource the Grid is displayed on the screen without any problems. Doubt: After this task I need the rows of the data grid to be colored according to the logic of my application. In that case I create a class called AcoesGrid where it will be responsible "as it already says the name" for doing any kind of action on the Grid. In this class is the code where the rows should be colored. For this I created a property of the Data grid in the class Acoes grid: WheredoIassigntheDatagridoftheForm.IntheFormcalledViewGridImakethereferencebetweenthepropertyandthedatagrid: AfterWorkingintheGridIreturntheclasspropertytotheform,"but by displaying the Form with Grid on the screen it continues with the DataSource information but without coloring the grid." > Here is the class code for better understanding:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using ppcp_protheus.Forms;
using System.Data.Odbc;
using ppcp_protheus.Acesso_a_Dados;
using System.Drawing;
using System.Data;
namespace ppcp_protheus
{
class clsAcoesGrid
{
//Propriedade criada para receber o Objeto Data Grid do formulario Visualizar Grid.
public DataGridView RefObjDgvPrincipal { get; set; }
List<clsPbx> listPbx = new List<clsPbx>();
DateTime dataBanco;
string email;
#region Métodos
public DataGridView colorirGrid(clsConexao dbCon)//, DataGridView dgvPrincipal)
{
//-------conectaBD.AbreXML();
List<clsProducao> listOpsAprov = new List<clsProducao>();
for (int i = 0; i < RefObjDgvPrincipal.Rows.Count; i++)
{
string operacao = RefObjDgvPrincipal[2, i].Value.ToString();
DateTime previsao = Convert.ToDateTime(RefObjDgvPrincipal[7, i].Value.ToString() + " " + RefObjDgvPrincipal[8, i].Value.ToString());
//DateTime previsao = Convert.ToDateTime("05/06/2018 01:52:00");
string ordem = RefObjDgvPrincipal[10, i].Value.ToString();
OdbcDataReader dr;
string id = "";
string ctde = "";
string ctpara = "";
string _sqlemail = "";
string _notificado = "";
foreach (clsPbx item in listPbx)
{
if (item._pbxCte == ordem)
{
id = item._pbxId;
ctde = item._pbxCte;
ctpara = item._pbxCtpara;
}
}
if (operacao.Contains("P"))
{
RefObjDgvPrincipal.Rows[i].DefaultCellStyle.BackColor = Color.Red;
}
else if (ordem.Substring(0, 2).Equals("AP"))
{
RefObjDgvPrincipal.Rows[i].DefaultCellStyle.BackColor = Color.Blue;
}
else if (previsao <= dataBanco)
{
RefObjDgvPrincipal.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
if (dbCon.VerificaStatusConexao() == ConnectionState.Closed)
dbCon.Conectar();
_sqlemail = "SELECT TOP 1 * FROM producao_SAP_V8 WITH(NOLOCK) WHERE ordemproducao = '" + ordem + "'";
dr = dbCon.RetornaDataReader(_sqlemail);
if (dr.HasRows)
{
_notificado = dr["notificado"].ToString();
}
dr.Dispose();
dr.Close();
if (_notificado != "1")
{
clsEnviarEmail enviarEmail = new clsEnviarEmail();
enviarEmail.EnviaEmail(email, "Apontamento de Produção", "A Ordem de Produção," + ordem + ",ultrapassou o tempo padrão ", "");
try
{
if (dbCon.VerificaStatusConexao() == ConnectionState.Closed)
dbCon.Conectar();
OdbcTransaction tran = dbCon.Connection.BeginTransaction();
string _exclui = String.Format("UPDATE producao_SAP_V8 Set notificado = '1' WHERE ordemproducao = '{0}'", ordem);
OdbcCommand odbcCMD = new OdbcCommand(_exclui, dbCon.Connection);
odbcCMD.Transaction = tran;
//odbcCMD.Parameters.Add("codigomotivo",OdbcType.VarChar,10).Value = motivo;
if (odbcCMD.ExecuteNonQuery() >= 1)
{
tran.Commit();
}
else
{
tran.Rollback();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
dbCon.FechaBanco();
}
}
dr.Close();
dr.Dispose();
}
else
{
RefObjDgvPrincipal.Rows[i].DefaultCellStyle.BackColor = Color.Lime;
}
if (ctde != "" && id != "")
{
if (ctpara.Equals("Gray"))
{
RefObjDgvPrincipal.Rows[i].DefaultCellStyle.BackColor = Color.Gray;
}
else if (ctpara.Equals("White"))
{
RefObjDgvPrincipal.Rows[i].DefaultCellStyle.BackColor = Color.White;
}
else if (ctpara.Equals("Orange"))
{
RefObjDgvPrincipal.Rows[i].DefaultCellStyle.BackColor = Color.Orange;
}
else if (ctpara.Equals("Purple"))
{
RefObjDgvPrincipal.Rows[i].DefaultCellStyle.BackColor = Color.Purple;
}
else if (ctpara.Equals("Brown"))
{
RefObjDgvPrincipal.Rows[i].DefaultCellStyle.BackColor = Color.Brown;
}
}
if (dbCon.Depto.Equals("03") && !operacao.Contains("P"))
{
string sqlAprov = String.Format(@"SELECT PC1.PC1_NUMOP AS ORDEM from PC1100 PC1 WITH(NOLOCK) where PC1.PC1_COD = '{0}'", ordem);
dr = dbCon.RetornaDataReader(sqlAprov);
while (dr.Read())
{
clsProducao prod = new clsProducao();
prod.OrdemProducao = dr["ORDEM"].ToString();
listOpsAprov.Add(prod);
}
dr.Dispose();
dr.Close();
foreach (clsProducao item in listOpsAprov)
{
if (item.OrdemProducao == ordem)
{
RefObjDgvPrincipal.Rows[i].DefaultCellStyle.BackColor = Color.LightBlue;
}
}
}
}
return RefObjDgvPrincipal;
}
#endregion
}
}
Follow the form code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ppcp_protheus.Forms;
namespace ppcp_protheus.Forms
{
public partial class frm_Visualizar_Grid : Form
{
clsConexao conectaBD = new clsConexao();
string setor;
public frm_Visualizar_Grid()//string depto)
{
InitializeComponent();
//setor = depto;
}
private void frm_Visualizar_Grid_Load(object sender, EventArgs e)
{
//Funcao do CarregarGrid
conectaBD.AbreXML(setor);
dgvUsinagem.AutoGenerateColumns = false;
dgvUsinagem.DataSource = clsPPCP.painelProducao(conectaBD);
clsAcoesGrid acoesGrid = new clsAcoesGrid();
//Enviando Objeto dgvUsinagem para Propriedade criada na classe clsAcoesGrid
acoesGrid.RefObjDgvPrincipal = this.dgvUsinagem;
//clsAcoesGrid.RefObjDgvPrincipal = this.dgvUsinagem;
acoesGrid.colorirGrid(conectaBD);
dgvUsinagem = acoesGrid.colorirGrid(conectaBD);//, dgvUsinagem);
//dgvUsinagem = clsAcoesGrid.colorirGrid(conectaBD);//, dgvUsinagem);
//dgvUsinagem.RowsDefaultCellStyle.BackColor = acoesGrid.RefObjDgvPrincipal.RowsDefaultCellStyle.BackColor;
//dgvUsinagem.RowsDefaultCellStyle.BackColor = Color.Red;
}
public void CarregaDepto(string depto)
{
setor = depto;
}
}
}
I do not know what I'm doing wrong if someone can help ...
I thank you for the help.
Thank you.