我目前有一个Datagridview,它可以从数据库中提取所有信息,并根据列的值和今天的日期之间的差异来获取行的颜色。它遍历网格并相应地为每行着色。我的问题是,当我希望它比较该特定行的下一步时,它为每一行比较同一列。它从数据库中获取当前作为int的位置,似乎正在获取第一行的位置并将其应用于所有行。
https://philipwalton.github.io/solved-by-flexbox/demos/vertical-centering/
这是我当前的代码:
private void seasonViewer_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
int x = 0;
string conString = "Host = xxxxx; Database = xxxx; username = xxxxx; password = xxxxx; Connection Timeout=15 ";
string query = "SELECT * FROM hamInfo;";
MySqlConnection conDatabase = new MySqlConnection(conString);
MySqlCommand cmdDatabase = new MySqlCommand(query, conDatabase);
try
{
conDatabase.Open();
MySqlDataReader myReader;
myReader = cmdDatabase.ExecuteReader();
while (myReader.Read())
{
x = myReader.GetInt32("location");
if (x <= 2)
{
x = 4;
}
else
{
x++;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
conDatabase.Dispose();
conDatabase.Close();
if (reportPickerBox.SelectedIndex == 0)
{
foreach (DataGridViewRow dr in seasonViewer.Rows)
{
if (dr.Cells[x].Value.ToString() != "")
{
DateTime date = DateTime.Now.Date;
DateTime nextStep = ((DateTime)dr.Cells[x].Value);
TimeSpan daysLeft = nextStep - date;
if (daysLeft.Days <= 2)
{
dr.DefaultCellStyle.BackColor = Color.DarkRed;
}
else if (daysLeft.Days <= 4 && daysLeft.Days >= 3)
{
dr.DefaultCellStyle.BackColor = Color.DarkGoldenrod;
}
else
dr.DefaultCellStyle.BackColor = Color.LightGreen;
}
}
}
}