I load an XML file, I transfer the data to the dataset and I display with the datagridview1.
I have 3 colums: start, end, and status.
The format of column start is datetime like 8:00 AM
The format of column end is datetime too like 10:00 PM
Status is two values: ok or nok.
I need to compare the datetime system with the start column and the end column, if the date system are between the two value, I display ok in the row. I need to do that for each row. for ok I need to change the background color to green and nok will be red.
could you help me? …. I m lost between the dataset and the datagridview.
thanks a lot
Thank you guys for your help : I modified a little the code, given by you 🙂
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Globalization;
using System.Security.Permissions;
namespace tab
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DataSet ds = new DataSet();
ds.ReadXml("C:\\Sites.xml");
int count = 0;
foreach (DataRow dr in ds.Tables[0].Rows)
{
string s1 = dr[0].ToString();
string s2 = dr[1].ToString();
string timeSys = DateTime.Now.ToString("hh:mm tt");
TimeSpan Start = DateTime.ParseExact(s1, "hh:mm tt", CultureInfo.InvariantCulture).TimeOfDay;
TimeSpan End = DateTime.ParseExact(s2, "hh:mm tt", CultureInfo.InvariantCulture).TimeOfDay;
TimeSpan Now = DateTime.ParseExact(timeSys, "hh:mm tt", CultureInfo.InvariantCulture).TimeOfDay;
if (Start.Hours < Now.Hours && Now.Hours < End.Hours)
{
ds.Tables[0].Rows[count][2] = "OK";
}
else
{
ds.Tables[0].Rows[count][2] = "NOK";
}
count++;
}
dataGridView1.DataSource = ds.Tables[0];
for (int icount = 0; icount < dataGridView1.RowCount-1; icount++)
{
DataGridViewRow theRow = dataGridView1.Rows[icount];
if (theRow.Cells[2].Value.ToString() == "OK")
theRow.Cells[2].Style.BackColor = Color.Green;
else
theRow.Cells[2].Style.BackColor = Color.Red;
}
}
}
}
I have still an issue, I didn’t see the color displayed in the datagridview.
1 Answer