1.dataGridView控件是一个绑定并显示数据表的控件,这是我写的一个简单的测试界面
2.展示简单的操作功能,视频如下:
Demo视频
3.代码功能实现如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static DataTable DT;
public static int Rowindex;
public static string path = "";//@"C:\Users\Administrator\Desktop\22.txt"
private void Form1_Load(object sender, EventArgs e)
{
path = textBox4.Text;
DT = Read_Point(path);
dataGridView1.Columns.Clear();
dataGridView1.DataSource = DT;
}
public static DataTable Read_Point(string path)
{
DataTable dt = new DataTable();
DataColumn col1 = new DataColumn("序号", typeof(int));
dt.Columns.Add(col1);
DataColumn col2 = new DataColumn("X", typeof(string));
dt.Columns.Add(col2);
DataColumn col3 = new DataColumn("Y", typeof(string));
dt.Columns.Add(col3);
DataColumn col4 = new DataColumn("Z", typeof(string));
dt.Columns.Add(col4);
try
{
using (StreamReader srIn = new StreamReader(path, Encoding.Default))
{
string msg = string.Empty;
int i = 0;
while (!srIn.EndOfStream)
{
msg = srIn.ReadLine();//下一行
string[] listIn;// = new string[7];
listIn = msg.Split(',');
if (listIn.Length < 2)
continue;
DataRow row = dt.NewRow();
row[0] = i;
row[1] = listIn[0];
row[2] = listIn[1];
row[3] = listIn[2];
//row[4] = listIn[3];
if (i <= dt.Rows.Count) i++;
else i = 0;
dt.Rows.Add(row);
}
}
}
catch (Exception ex)
{
}
return dt;
}
public static void Write_configload(string path)
{
if (File.Exists(path))
{
File.Delete(path);
}
using (FileStream fs = new FileStream(path, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite))
{
using (StreamWriter sw = new StreamWriter(fs))
{
//读txt文件,并把配置信息写入内存
for (int i = 0; i < DT.Rows.Count; i++)
{
string temp = DT.Rows[i]["X"].ToString()+","+ DT.Rows[i]["Y"].ToString()+","+DT.Rows[i]["Z"].ToString();
sw.WriteLine(temp);
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
//string temp = textBox1.Text + "," + textBox2.Text + "," + textBox3.Text;
DataRow row = DT.NewRow();
row[0] = DT.Rows.Count;
row[1] = textBox1.Text;
row[2] = textBox2.Text;
row[3] = textBox3.Text;
//DataPoint.Add(temp);
DT.Rows.Add(row);
}
private void button2_Click(object sender, EventArgs e)
{
DataRow row = DT.NewRow();
row[0] = Rowindex;
row[1] = textBox1.Text;
row[2] = textBox2.Text;
row[3] = textBox3.Text;
DT.Rows.InsertAt(row, Rowindex);
for (int i = 0; i < DT.Rows.Count; i++)
{
DT.Rows[i][0]=i;
}
}
private void button3_Click(object sender, EventArgs e)
{
DT.Rows.RemoveAt(Rowindex);
for (int i = 0; i < DT.Rows.Count; i++)
{
DT.Rows[i][0] = i;
}
}
private void button4_Click(object sender, EventArgs e)
{
DT.Rows.Clear();
}
private void button5_Click(object sender, EventArgs e)
{
Write_configload(path);
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
Rowindex = e.RowIndex;
}
}
}
4.若有疑问,留言沟通,感谢浏览!