我需要用时间值填充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分
请参考
输出时间值的输出和输出下面给出函数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();
}
}
你能给我们一个你正在导入的文件的例子吗? – 2013-05-09 08:27:12
您正在填写两栏。两列都显示不正确的值还是只有一个?哪一列显示不正确的值。你能举一个你期望的和你看到的例子吗? – 2013-05-09 08:29:18
您正在为您的for循环内的'dataGridView1.Rows [n]'赋值,而不是'dataGridView1.Rows [i]'。由大脑编译器可能会扭曲,但不会不断地将值应用到同一行的单元格0和1?对我来说,你最好在for循环中使用'dataGridView1.Rows.Add(TestTime,split [i])'而不是两个当前的赋值。 – Adrian 2013-05-09 08:34:50