I'm having the most crazy issue ever I have one tabcontrol where I add a tabpage then create a datagridview filling it with a query as datasource and make some visual changes on it and then make the same thing for another tabpage.
I run the code and somehow I get the data I want showing in the gridviews everything fine except the visual changes in the second one. I go debugging and end up finding out that the visual changes aren't happening because technically the query return the second datagridview gets as datasource is = {} so it has no rows to make visual changes on. but the data is showing on the datagridview anyway. I tried changing the order of the datagridviews and it was the very same issue, so I think it's something about the order the code is but I don't understand what nor where exactly.
I'm working on Visual Studio 2012 in C#, accessing the data of a Access 2010 file. And here is the code in question:
///Tabela por calibrar
DataTable tabelaEqui = null;
tabelaEqui = calibracaoBL.planoCalibracaoAtualCalibradosServico(DateTime.Now.Year, servico);
DataGridView gridPorCalibrar = new DataGridView();
gridPorCalibrar.DataSource = tabelaEqui;
tabControl1.TabPages.Add(cbServico.Text + " - Por Calibrar");
tabControl1.TabPages[0].Controls.Add(gridPorCalibrar);
gridPorCalibrar.Dock = DockStyle.Fill;
gridPorCalibrar.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
gridPorCalibrar.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
for (int i = 0; i < gridPorCalibrar.Rows.Count - 1; i++)
{
for (int j = 5; j < gridPorCalibrar.Rows[i].Cells.Count; j++)
{
string s = gridPorCalibrar.Rows[i].Cells[j].Value.ToString();
Boolean b = !s.Equals("");
if (b)
{
gridPorCalibrar.Rows[i].Cells[j].Style.BackColor = Color.Green;
}
}
}
mudarCabeçalho(gridPorCalibrar);
///Tabela Calibrados
DataTable dt = null;
dt = calibracaoBL.planoCalibracaoAnosAnterioresServico(DateTime.Now.Year, servico);
DataGridView gridCalibrados = new DataGridView(); // here I got check and dt = {}
gridCalibrados.DataSource = dt;
tabControl1.TabPages.Add(cbServico.Text + " - Calibrados");
tabControl1.TabPages[1].Controls.Add(gridCalibrados);
gridCalibrados.Dock = DockStyle.Fill;
gridCalibrados.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
gridCalibrados.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
for (int i = 0; i < gridCalibrados.Rows.Count - 1; i++)
{
for (int j = 5; j < gridCalibrados.Rows[i].Cells.Count; j++)
{
string s = gridCalibrados.Rows[i].Cells[j].Value.ToString();
Boolean b = !s.Equals("");
if (b)
{
gridCalibrados.Rows[i].Cells[j].Style.BackColor = Color.Green;
}
}
}
mudarCabeçalho(gridCalibrados);
Here is the first one:
Here is the second one:
UPDATE in response to @varocarbas :
I'm checking the datasource is blank in two points. Point one: dt = calibracaoBL.planoCalibracaoAnosAnterioresServico(DateTime.Now.Year, servico); // that method that runs a query returns {}. But the query itself is not the issue since it works just fine, it's working perfectly when I try it elsewhere.
Point two: DataTable dt = null; dt = calibracaoBL.planoCalibracaoAnosAnterioresServico(DateTime.Now.Year, servico); DataGridView gridCalibrados = new DataGridView(); // here I go check the dt variable and shows dt={}
The code goes into the for (int i = 0; i < gridCalibrados.Rows.Count - 1; i++) but since gridCalibrados.Rows.Count = 0 it skips the loop.
Here is the method mudarCabeçalho
public void mudarCabeçalho(DataGridView grid)
{
for (int i = 5; i < grid.ColumnCount; i++)
{
string[] s = grid.Columns[i].HeaderText.Split('-');
switch (s[1])
{
case "01":
grid.Columns[i].HeaderText = "Jan";
break;
case "02":
grid.Columns[i].HeaderText = "Fev";
break;
case "03":
grid.Columns[i].HeaderText = "Mar";
break;
case "04":
grid.Columns[i].HeaderText = "Abr";
break;
case "05":
grid.Columns[i].HeaderText = "Mai";
break;
case "06":
grid.Columns[i].HeaderText = "Jum";
break;
case "07":
grid.Columns[i].HeaderText = "Jul";
break;
case "08":
grid.Columns[i].HeaderText = "Ago";
break;
case "09":
grid.Columns[i].HeaderText = "Set";
break;
case "10":
grid.Columns[i].HeaderText = "Out";
break;
case "11":
grid.Columns[i].HeaderText = "Nov";
break;
case "12":
grid.Columns[i].HeaderText = "Dez";
break;
}
}
}