2013-05-09 60 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