我需要用间隔为 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();
}
}