Previous Article: html "> http://www.bkjia.com/kf/201104/88559.html
Now, I have solved two of my own problems and made some minor changes.
This is the improved running effect:
First, read and display the data after the third row.
Simply, a counter count is defined. The while LOOP reads the text first, and when count is 3, the actually reads what we need.
Int count = 0;
While (strLine! = Null)
{
StrLine = sr. ReadLine ();
Count ++;
If (count = 3)
{
While (strLine! = Null)
{
String [] strArry = strLine. Split ();
DataRow dr = dt. NewRow ();
For (int I = 0; I <= strArry. Length-1; I ++)
{
Dr [I] = strArry [I];
}
Dt. Rows. Add (dr );
StrLine = sr. ReadLine ();
}
Break;
}
Second, the data in the second row is read and used as the column name. This cannot be done. At least I don't know how to do it now. As we have already solved the problems we need, we should not think about it any more and there should be no solutions.
In the running effect, there is a Button for opening the file and a Textbox (used to display the file path ).
This is the basic code for opening a file:
Private void OpenFile ()
{
OpenFileDialog openFile = new OpenFileDialog ();
OpenFile. Title = "open ";
OpenFile. FileName = ""; // file name
OpenFile. InitialDirectory = Environment. GetFolderPath (Environment. SpecialFolder. MyComputer); // enable my computer by default
OpenFile. Filter = "text file (*. txt) | *. txt"; // file type
OpenFile. ValidateNames = true ;//
OpenFile. CheckFileExists = true ;//
OpenFile. CheckPathExists = true ;//
If (openFile. ShowDialog () = DialogResult. OK)
{
// DisplayData (openFile. FileName );
TxtFilePath. Text = openFile. FileName;
}
}
These code for opening files may be more comprehensive online.
The changed source code is not posted, and it is very simple.