2013-05-09 14 views
0

我需要用時間值填充datagridview中的列,間隔爲30分鐘。我有開始時間和時間間隔。我儘量使用下面的代碼,但它只輸出列的每個單元格的開始時間。你能否幫我以30分鐘的時間間隔檢索所需的時間值。在此先感謝檢索一組具有時間間隔的DateTime值

string line; 
System.IO.StreamReader file = new System.IO.StreamReader("test.txt"); 

     while ((line = file.ReadLine()) != null) 
     { 
       //Reading the line which contains DISKXFER. Eg:"DISKXFER,T0001,0.5,0.0" 
       if (line.Contains("DISKXFER")) 
      { 
       //split the line and insert to an array, Eg: split[0]=DISKXFER 
       string dataLine = line.ToString(); 
       string[] split = dataLine.Split(','); 
       int result = split.Length; 

       //get the date at line 12. The date starts from 9th string and has a length of 11. 
       string lineDate = GetLine(@"test.txt", 12); 
       string getDate=lineDate.Substring(9,11); 

       //get the start time at line 11. The timee starts from 9th string and has a length of 8. 
       string lineTime = GetLine(@"test.txt", 11); 
       string getTime=lineTime.Substring(9,8); 

       //merge date and time 
       DateTime TestTime = DateTime.Parse(getDate + " " + getTime);      


       int n = dataGridView1.Rows.Add(); 
       //split[] has four elements. Add those elements to four columns in the same row and continue with other lines 
       for (int i = 0; i < result; i++) 
       { 
        //add testtimes to the first column 
        dataGridView1.Rows[n].Cells[0].Value = TestTime; 
        TestTime = TestTime.AddMinutes(30); 
        //add split array to other columns in the same row of testtime. Eg: split[0] to column2, split[1] to column3, split[2] to column4, split[3] to column5 
        dataGridView1.Rows[n].Cells[1].Value = split[i];   

       }     

      } 

     } 
     file.Close(); 

感謝您的意見。我會附上我收到我需要 ,我需要的是像這樣因爲開始時間是上午12時25分 enter image description here

請參考 A

輸出時間值的輸出和輸出下面給出函數getline方法:

public string GetLine(string fileName, int line) 
    { 
     using (System.IO.StreamReader ssr = new System.IO.StreamReader("test.txt")) 
     { 
      for (int i = 1; i < line; i++) 
       ssr.ReadLine(); 
      return ssr.ReadLine(); 
     } 
    } 
+0

你能給我們一個你正在導入的文件的例子嗎? – 2013-05-09 08:27:12

+0

您正在填寫兩欄。兩列都顯示不正確的值還是隻有一個?哪一列顯示不正確的值。你能舉一個你期望的和你看到的例子嗎? – 2013-05-09 08:29:18

+0

您正在爲您的for循環內的'dataGridView1.Rows [n]'賦值,而不是'dataGridView1.Rows [i]'。由大腦編譯器可能會扭曲,但不會不斷地將值應用到同一行的單元格0和1?對我來說,你最好在for循環中使用'dataGridView1.Rows.Add(TestTime,split [i])'而不是兩個當前的賦值。 – Adrian 2013-05-09 08:34:50

回答

1

基於關閉您的意見和您所提供的示例行,這應該給你你要找的輸出:因此,所有我在這裏做的

string line; 
System.IO.StreamReader file = new System.IO.StreamReader("test.txt"); 

//get the date at line 12. The date starts from 9th string and has a length of 11. 
string lineDate = GetLine(@"test.txt", 12); 
string getDate=lineDate.Substring(9,11); 

//get the start time at line 11. The timee starts from 9th string and has a length of 8. 
string lineTime = GetLine(@"test.txt", 11); 
string getTime=lineTime.Substring(9,8); 

//merge date and time 
DateTime TestTime = DateTime.Parse(getDate + " " + getTime); 

while ((line = file.ReadLine()) != null) 
{ 
     //Reading the line which contains DISKXFER. Eg:"DISKXFER,T0001,0.5,0.0" 
     if (line.Contains("DISKXFER")) 
     { 
      //split the line and insert to an array, Eg: split[0]=DISKXFER 
      string dataLine = line.ToString(); 
      string[] split = dataLine.Split(','); 
      int result = split.Length; 

      int n = dataGridView1.Rows.Add(); 
      //split[] has four elements. Add those elements to four columns in the same row and continue with other lines 

      //add testtimes to the first column 
      dataGridView1.Rows[n].Cells[0].Value = TestTime; 
      for (int i = 0; i < result; i++) 
      { 
       //add split array to other columns in the same row of testtime. Eg: split[0] to column2, split[1] to column3, split[2] to column4, split[3] to column5 
       dataGridView1.Rows[n].Cells[i + 1].Value = split[i];   

      } 

      TestTime = TestTime.AddMinutes(30); 
     } 
    } 
file.Close(); 

:移動的初始原料與材料外設置while循環(因爲否則你總是會有相同的時間,而不是你說你正在尋找的連續30分鐘的增量),移動添加TestTime到for循環之外的列,因爲你只需要每次循環執行一次行,然後將您的參考從dataGridView1.Rows[n].Cells[1]更改爲dataGridView1.Rows[n].Cells[i + 1],以便您將其他數據點添加到一排。一旦for循環完成,然後我們添加30分鐘並繼續到下一行。

+0

它的工作原理。但它增加了150分鐘而不是30分。無論如何,它可能與提供的問題無關。我會盡力找到解決方案。非常感謝您的正確答案。 – 2013-05-10 06:45:15

+0

您是否確定將'TestTime = TestTime.AddMinutes(30);'移出for循環? – Adrian 2013-05-10 06:47:49

+0

是的,我把它移出了 – 2013-05-10 06:54:46

0

你在你的代碼的循環:

for (int i = 0; i < result; i++) 
{ 
    dataGridView1.Rows[n].Cells[0].Value = TestTime; 
    TestTime = TestTime.AddMinutes(30); 
    dataGridView1.Rows[n].Cells[1].Value = split[i];   
} 

對於輸入行中的每個「分割」,循環體都被執行。但是,不會添加新行,並且在每次迭代中分配相同的兩列(0和1)。

從這個問題你想要什麼來實現,但你可能需要解決這個循環還不清楚:

  • 如果每一個「分裂」應該建立在數據集中,然後新的一行添加此行在循環體中。

  • 如果每一個「分裂」應該將數據添加到數據一雙新列的設置,你需要使用列索引2*i2*i + 1,而不是固定的01

+0

同樣的事情說了兩種不同的方式,只需幾秒鐘:D – Adrian 2013-05-09 08:36:27