2011-12-23 80 views
0

我加載一個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的。

+0

這個問題需要一個有意義的標題,一個不只是標籤的重複,但我不要對這個問題的理解不夠充分,無法提供。有人? – Amy 2011-12-23 06:35:32

+0

如果顏色問題是唯一剩下的問題,請打開一個新問題。看看CellFormatting事件。這是定義單元格顏色的正確時機(例如,通過e.CellStyle.BackColor)。 – TheBlastOne 2011-12-23 17:23:49

+0

如果這種方法奏效,請記住標記爲答案(使用答案旁邊的勾號) – V4Vendetta 2012-01-05 10:06:33

回答

0
 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; 
     } 

    } 
+0

接下來你需要的是XML文件,其餘的是條件的變化。 – Pratik 2011-12-23 09:33:48

+0

是的,我修改了條件,它是完美的。我仍然有問題要顯示顏色。我需要爲此激活一些東西嗎? – user1112847 2011-12-23 15:52:10

+0

否,因爲您使用Windows窗體不需要激活任何事情。 – Pratik 2012-01-09 10:21:46

0

您可以嘗試通過行訂閱到DataBindingComplete事件在DataGridView的

然後循環

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; 

您現在可以設置使用上有所這些行單元格顏色

if(now > start && now < end) 
    row.Cells[colname].Style.BackColor = Color.Green; 
+0

我無法編輯帖子,稍後將添加詳細信息 – V4Vendetta 2011-12-23 07:17:23

+0

這是假設您有時間在適當的區域否則它將最適合現在變成'.UtcNow'。 – V4Vendetta 2011-12-23 08:50:57

+0

非常感謝你的幫助:) – user1112847 2011-12-23 15:52:32