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.
You can try subscribing to the DataBindingComplete
event of the DataGridView
Then loop through the rows
string s1 = "08:00 AM"; // this corresponds to your start value in grid
string s2 = "10:00 PM";
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.Now.TimeOfDay;
you can now set the cell color using somewhat on these lines
if(now > start && now < end)
row.Cells[colname].Style.BackColor = Color.Green;
DataSet ds = new DataSet();
ds.ReadXml(@"...\\..\\Sites.xml");
int count = 0;
foreach (DataRow dr in ds.Tables[0].Rows)
{
string s1 = dr[0].ToString();
string s2 = dr[1].ToString();
TimeSpan ts1 = DateTime.ParseExact(s1, "hh:mm tt", CultureInfo.InvariantCulture).TimeOfDay;
TimeSpan ts2 = DateTime.ParseExact(s2, "hh:mm tt", CultureInfo.InvariantCulture).TimeOfDay;
TimeSpan now = DateTime.Now.TimeOfDay;
if (ts1.Hours >= 8 && ts2.Hours <= 22)
{
ds.Tables[0].Rows[count][2] = "OK";
}
else
{
ds.Tables[0].Rows[count][2] = "NOK";
}
count++;
}
grdvw_wnfrm.DataSource = ds.Tables[0];
for (int icount = 0; icount < grdvw_wnfrm.RowCount-1; icount++)
{
DataGridViewRow theRow = grdvw_wnfrm.Rows[icount];
if (theRow.Cells[2].Value.ToString() == "OK")
theRow.Cells[2].Style.BackColor = Color.Green;
else
theRow.Cells[2].Style.BackColor = Color.Red;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.