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 DataGridView
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonTest_Click(object sender, EventArgs e)
{
//新增加一列
//dataGridView1.Columns.Add("age", "年龄");
//修改行和列 (属性能读能写)
//dataGridView1.ColumnCount = 6;
//dataGridView1.RowCount = 8;
//添加数据
//dataGridView1.Rows.Add("龙珠","悟饭","男","爆发潜力");
//修改行头名称
//dataGridView1.Columns[0].HeaderText = "修改";
}
private void buttonXiuGai_Click(object sender, EventArgs e)
{
//修改行列内容
//dataGridView1.Rows[0].Cells[1].Value = "布罗利";
//清空所有内容
//dataGridView1.Rows.Clear();
//删除指定位置一行
//dataGridView1.Rows.RemoveAt(0);
//删除指定位置一行
dataGridView1.Rows.Remove(dataGridView1.SelectedRows[0]);
}
private void Form1_Load(object sender, EventArgs e)
{
//修改行列内容
dataGridView1.Rows[0].Cells[4].Value = "ON";
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.SelectedCells[0].Value.ToString() == "ON")
{
dataGridView1.SelectedCells[0].Value= "OFF";
} else if (dataGridView1.SelectedCells[0].Value.ToString() == "OFF")
{
dataGridView1.SelectedCells[0].Value = "ON";
}
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog(); //弹出来一个文件筐
openFileDialog.InitialDirectory = Directory.GetCurrentDirectory();
//打印当前位置
//Console.WriteLine(openFileDialog.InitialDirectory.ToString());
openFileDialog.Filter = "Csv文件(*.csv)|*.csv";//设置过滤器
//openFileDialog.ShowDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(openFileDialog.FileName, Encoding.Default);
//修改行和列 (属性能读能写)
dataGridView1.ColumnCount = 3;
dataGridView1.RowCount = 4;
for (int i = 0; i < dataGridView1.RowCount; i++)
{
string oldString = sr.ReadLine();
string[] newString = oldString.Split(new char[3] { ',', ',', ',' });
for (int k = 0; k < dataGridView1.ColumnCount; k++)
{
dataGridView1.Rows[i].Cells[k].Value = newString[k];
}
}
}
}
}
}
DataGridView
最新推荐文章于 2024-11-14 17:50:52 发布