当前位置:网站首页>C reads the data in the CSV file and displays it after importing the DataTable
C reads the data in the CSV file and displays it after importing the DataTable
2022-07-28 20:39:00 【laocooon】
1 99 99 99
2 88 88 88
3 77 77 77
4 66 66 66
5 55 55 55
6 44 44 44
7 33 33 33
8 22 22 22
9 11 11 11
10 0 0 0
private void button4_Click(object sender, EventArgs e)
{
string fileName = string.Empty;
string strFilePath = System.IO.Directory.GetCurrentDirectory();
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = strFilePath;
openFileDialog.Filter = "(*.CSV)|*.CSV";
openFileDialog.Title = " Open data file ";
DialogResult result = openFileDialog.ShowDialog();
if (result != DialogResult.OK)
return;
List<string> listA=new List<string>();
List<string> listB = new List<string>();
List<string> listC = new List<string>();
fileName = openFileDialog.FileName;
using (var reader = new StreamReader(fileName))
{
while (!reader.EndOfStream)
{
var line= reader.ReadLine();
var values= line.Split('\t');
if (values.Length != 3)
{
MessageBox.Show(
" Not a legal data source , The program failed to import data ~!",
" Prompt information ",
MessageBoxButtons.OK,
MessageBoxIcon.Warning
);
return;
}
listA.Add(values[0]);
listB.Add(values[1]);
listC.Add(values[2]);
}
}
string[,] data = new string[listA.Count,3];
for (int i = 0; i < listA.Count; i++)
{
data[i,0] = listA[i];
data[i, 1] = listB[i];
data[i, 2] = listC[i];
Console.WriteLine($"{data[i, 0]} {data[i, 1]} {data[i, 2]}");
}
// Two dimensional array turn DataTable data source
DataTable dt = new DataTable();
for (int i = 0; i < data.GetLength(1); i++)
dt.Columns.Add(i.ToString(), typeof(int));
for (int i = 0; i < data.GetLength(0); i++)
{
DataRow dr = dt.NewRow();
for (int j = 0; j < data.GetLength(1); j++)
dr[j] = data[i, j];
dt.Rows.Add(dr);
}
dataGridView1.DataSource = dt;
}边栏推荐
猜你喜欢

激光slam:LeGO-LOAM---代码编译安装与gazebo测试

一文了解 Rainbond 云原生应用管理平台

js图片悬挂样式照片墙js特效

太空射击第10课: Score (繪畫和文字)

Raspberry connects EC20 for PPP dialing

The engineering practice of super large model was polished, and Baidu AI Cloud released the cloud native AI 2.0 solution

Raspberry pie CM4 -- using metartc3.0 to integrate ffmpeg to realize webrtc push-pull streaming

Windows系统下Mysql数据库定时备份

Configure Windows Server + install MySQL database on the server + Remote Access database

Introduction to seven kinds of polling (practice link attached)
随机推荐
产品经理访谈 | 第五代验证码的创新与背景
Talking about canvas and three rendering modes in unity
Install keras, tensorflow, and add the virtual environment to the Jupiter notebook
Anaconda creation environment
C语言简单实例 1
Vivado designs PC and ram
Pop up modal box
MySQL startup error 1607 unexpected process termination
Solve the problem that jupyter cannot import new packages
C language data 3 (2)
动态规划:背包问题模板代码汇总
Raspberry pie 3b ffmpeg RTMP streaming
【CodeForces】Educational Codeforces Round 132 (Rated for Div. 2)
Data mining (data preprocessing) -- Notes
CNN convolution neural network learning process (weight update)
Use of DDR3 (axi4) in Xilinx vivado (2) read write design
[link url]
太空射击第10课: Score (繪畫和文字)
Voice controlled robot based on ROS (II): implementation of upper computer
Linxu [permission, sticky bit]