'add an image into datagridview based on a condition
i want to show an image in datagridview based on a condition (if the endday of subscribtion is < today then show a red cross image else show a checkmark image) , i have an sqlite database and i created a datagridviewimagecolumn called expired to shopw this images , but i'm not getting anything , here is the code
``for (int row = 0; row <= membersdashboarddatagrid.Rows.Count - 1; row++)
{
DateTime endday = Convert.ToDateTime(membersdashboarddatagrid.CurrentRow.Cells[3].Value);
if (endday < DateTime.Today)
{
((DataGridViewImageCell)membersdashboarddatagrid.Rows[row].Cells[0]).Value = Properties.Resources.delete;
}
else
{
((DataGridViewImageCell)membersdashboarddatagrid.Rows[row].Cells[0]).Value = Properties.Resources._checked;
}
}`
Solution 1:[1]
The problem you are having is that in each iteration of the for loop, the code is checking the “SAME” Cells EndDay value. Specifically, the line of code…
DateTime endday = Convert.ToDateTime(membersdashboarddatagrid.CurrentRow.Cells[3].Value);
The portion of this that is using the same cell is…
membersdashboarddatagrid.CurrentRow.Cells[3].Value
The code above uses the SAME CurrentRow.Cell[3].Value with each iteration. Therefore, all the Expire images will be the same depending on what the CurrentRows EndDay value is. I am confident you want something like…
membersdashboarddatagrid.Rows[row].Cells[3].Value);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | JohnG |

