我需要用時間值填充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