Using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
Using System. The Data. SqlClient.
Using System. IO;
The namespace WindowsFormsApplication1
{
Public partial class Form1: Form
{
Public _click ()
{
InitializeComponent ();
}
Private void button1_Click (object sender, EventArgs e)
{
Using (OpenFileDialog dlgText=new OpenFileDialog ())
{
DlgText. Filter="text file | *. TXT";
If (dlgText ShowDialog ()==DialogResult. OK)
{
//create a datatable used to hold data read
DataTable dt=new DataTable();
//add three columns to the datatable
Dt. Columns. The Add (" ID ", typeof (String));
Dt. Columns. The Add (" Name ", typeof (String));
Dt. Columns. The Add (" level ", typeof (String));
Dt. Columns. The Add (" nextlevelexp typeof (String));
Dt. Columns. The Add (" BodyRadius typeof (String));
Dt. Columns. The Add (" BaseWalkSpeed typeof (String));
Dt. Columns. The Add (" BaseMaxHp typeof (String));
Dt. Columns. The Add (" BaseAtkSpeed typeof (String));
Dt. Columns. The Add (" BaseNormalAtk typeof (String));
Dt. Columns. The Add (" BaseNormalDef typeof (String));
Dt. Columns. The Add (" BaseHitValue typeof (String));
Dt. Columns. The Add (" BaseDodgeValue typeof (String));
Dt. Columns. The Add (" BaseCritValue typeof (String));
Dt. Columns. The Add (" BaseCritAvoidValue typeof (String));
//read the file
Using (StreamReader reader=new StreamReader (dlgText FileName, Encoding. The Default))
{
//loop reads all rows
while (! Reader. EndOfStream)
{
//will each row data, using - divided into 3 section
String [] data=(https://bbs.csdn.net/topics/reader.ReadLine). The Replace (" ", "-"). The Split (' - ');
//on a new line, and will read data segmentation, respectively into three corresponding column
If (data. The Length==14)
{
DataRow Dr=dt. NewRow ();
//int index=dt. Rows. The Add ();
Dr [0]=data [0];
Dr [1]=data [1];
Dr [2]=data [2];
Dr [3]=data [3].
Dr [4]=data [4];
Dr [5]=data [5];
Dr [6]=data [6].
Dr [7]=data [7];
Dr [8]=data [8];
Dr [9]=data [9];
Dr [10]=data [10].
Dr [11]=data [11];
Dr [12]=data [12].
Dr [13]=data [13].
//the row data join the datatable
Dt. Rows. The Add (Dr);
}
DataGridView1. The DataSource=dt;
}
}
//will be displayed on the datatable binding to the datagridview results
Enclosing dataGridView1. The DataSource=dt;
}
}
}
Private void dataGridView1_CellContentClick (object sender, DataGridViewCellEventArgs e)
{
//dataGridview1. Rows. The add (item [index])
}
Private void dataGridView1_CellContentClick_1 (object sender, DataGridViewCellEventArgs e)
{
}
}
}
CodePudding user response:
There are many lines, many columns, what to readCodePudding user response:
The contents of a file. The readAll can directly read the whole fileEach line in the split space data,
Looks like you have achieved, why do you ask
CodePudding user response:
Read but not straight, I'm going to read a lot of a TXT file, the only one, and I show no columns of dataCodePudding user response:
To open a casually excel file to display tabular form, application configuration file that gameCodePudding user response: