0
I have 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 Datagrid.Datasource the Grid is displayed on the screen smoothly.
Doubt:
After this task I need the data grid rows to be colored according to the logic of my application.
In this case I create a class called Acoesgrid where she will be responsible " as the name already says" 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 Data grid property in the Acoes grid class:
Where I assign the Form Data Grid.
In the form called View Grid I make the reference between the property and the data grid:
After Working on the Grid I return the class property to the form, "but by showing the Form with Grid on the screen it continues with the Datasource information but without coloring the grid."
Follow 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 code of the Form:
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 don’t know what I’m doing wrong if anyone can help...
I leave my thanks for the help.
Thank you.
Apparently you have created a method that returns another Datagridview, calls the method but does not apply the return anywhere. Also the code is very confusing...
– Rovann Linhalis
by the way, you have another question that was not solved or you did not mark as answer: https://answall.com/questions/307928/chamar-form2-com-circular-progress-bar-enquanto-outra-a%C3%A7%C3%A3o-%C3%A9-executed-c
– Rovann Linhalis
Sorry I thought I had already selected the question: https://answall.com/questions/307928/chamar-form2-com-circular-progress-bar-enquanto-outra-a%C3%A7%C3%A3o-%C3%A9-executed-c
– William
the Return of this method: public Datagridview colorirGrid(clsConexao dbCon) { } Return Refobjdgvprincipal; "Wasn’t to return the datagrid to the form? form code: dgvUsinagem = acoesGrid.colorirGrid(conectaBD);?
– William
if it’s returning, it should be assigned, something like
dgvUsinagem = acoesGrid.colorirGrid(conectaBD);
but it makes no sense to do this... try to separate better what each method does... and return the logic of colors already in the select of the data– Rovann Linhalis
How ? can you give me an example? return the colors of the datagril property to another datagrid?
– William
what is
clsProducao
andclsPbx
? What is the logic for applying colors ?– Rovann Linhalis
The logic for applying the colors is in nos if / Else for example the operation contain substring "P" means that in my base it brings an operation of Equipment Shutdown.
– William
Then the line will be colored by one color. If the construction forecast of the part exceeds the date bank fills the line with another color and so on, there are several in the block of conium. clsProduction brings a list of Ops= production order with taking notes. Take me awayOur doubt is right I do the property of type data grid and send the datagrid to another class and then return the way I did or did not exist better applications?
– William
see if the answer helps...
– Rovann Linhalis