我加載一個XML文件,我將數據傳輸到數據集,然後使用datagridview1顯示。Datagridview格式問題
我有3個colums:開始,結束和狀態。
柱開始的格式是這樣8:00 AM
柱端的格式的日期時間爲datetime太喜歡10:00 PM
狀態是兩個值:OK或NOK。
我需要比較日期時間系統與開始列和結束列,如果日期系統在兩個值之間,我在行中顯示ok。我需要爲每一行都做到這一點。好的,我需要將背景顏色更改爲綠色,並且nok將變爲紅色。 你能幫助我嗎? ....我失去了數據集和datagridview之間。 非常感謝
謝謝你們的幫助:我修改了一個小的代碼,你:)
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;
}
}
}
}
給我還有一個問題,我沒有看到顯示的顏色datagridview的。
這個問題需要一個有意義的標題,一個不只是標籤的重複,但我不要對這個問題的理解不夠充分,無法提供。有人? – Amy 2011-12-23 06:35:32
如果顏色問題是唯一剩下的問題,請打開一個新問題。看看CellFormatting事件。這是定義單元格顏色的正確時機(例如,通過e.CellStyle.BackColor)。 – TheBlastOne 2011-12-23 17:23:49
如果這種方法奏效,請記住標記爲答案(使用答案旁邊的勾號) – V4Vendetta 2012-01-05 10:06:33