0

我需要用间隔为 30 分钟的时间值填充 datagridview 中的一列。我有开始时间和时间间隔。我努力使用下面的编码,但它只输出列的每个单元格的开始时间。您能帮我以 30 分钟的间隔检索所需的时间值吗?提前致谢

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 在此处输入图像描述

请参考下面给出的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();
        }
    }
4

2 回答 2

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(); 

所以我在这里所做的就是:将初始 TestTime 集移到 while 循环之外(因为否则你将始终拥有相同的时间,而不是你说你正在寻找的连续 30 分钟增量),将 TestTime 添加到列之外在 for 循环中,因为您只需要每行执行一次,然后将引用从 更改为dataGridView1.Rows[n].Cells[1]dataGridView1.Rows[n].Cells[i + 1]以便将其他数据点添加到一行的连续列中。一旦 for 循环完成,然后我们添加 30 分钟并继续下一行。

于 2013-05-10T04:44:25.480 回答
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不是固定的0and 1

于 2013-05-09T08:35:01.007 回答