DataGridView

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];
                    }
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值