• 大小: 0.12M
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2020-12-25
  • 语言: C#
  • 标签: C#  DataGridView  

资源简介

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;

namespace DataGridViewSample
{
    public partial class Form1 : Form
    {
        public static List<Test> Tests = new List<Test>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.dataGridView1.DataSource = Tests;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            frmAdd form = new frmAdd(null);
            if (form.ShowDialog() == DialogResult.OK)
            {
                this.dataGridView1.DataSource = null;
                this.dataGridView1.DataSource = Tests;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows != null)
            {
                frmAdd form = new frmAdd(new Test(
                    Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells[0].Value),
                    Convert.ToDecimal(this.dataGridView1.SelectedRows[0].Cells[1].Value),
                    Convert.ToDateTime(this.dataGridView1.SelectedRows[0].Cells[2].Value),
                    this.dataGridView1.SelectedRows[0].Cells[3].Value.ToString()));
                if (form.ShowDialog() == DialogResult.OK)
                {
                    this.dataGridView1.DataSource = null;
                    this.dataGridView1.DataSource = Tests;
                }
            }            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //使用前面示例中的代码,用DataGridView或者List<Test>来做导出都是可以的
        }
    }

    public class Test
    {
        public Test(int id, decimal voltage, DateTime record, string remark)
        {
            ID = id;
            Voltage = voltage;
            Record = record;
            Remark = remark;
        }

        public int ID { get; set; }
        public decimal Voltage { get; set; }
        public DateTime Record { get; set; }
        public string Remark { get; set; }
    }
}

资源截图

代码片段和文件信息

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;

namespace DataGridViewSample
{
    public partial class Form1 : Form
    {
        public static List Tests = new List();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender EventArgs e)
        {
            this.dataGridView1.DataSource = Tests;
        }

        private void button1_Click(object sender EventArgs e)
        {
            frmAdd form = new frmAdd(null);
            if (form.ShowDialog() == DialogResult.OK)
            {
                this.dataGridView1.DataSource = null;

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件         896  2015-05-26 15:43  DataGridViewSample\DataGridViewSample.sln
     文件       20992  2015-05-26 15:43  DataGridViewSample\DataGridViewSample.suo
     文件       46080  2015-06-11 09:35  DataGridViewSample\DataGridViewSample.v12.suo
     目录           0  2015-05-27 07:39  DataGridViewSample\DataGridViewSample\
     目录           0  2015-05-26 15:43  DataGridViewSample\DataGridViewSample\bin\
     目录           0  2015-05-26 15:43  DataGridViewSample\DataGridViewSample\bin\Debug\
     文件       14848  2015-05-27 07:39  DataGridViewSample\DataGridViewSample\bin\Debug\DataGridViewSample.exe
     文件       40448  2015-05-27 07:39  DataGridViewSample\DataGridViewSample\bin\Debug\DataGridViewSample.pdb
     文件       23168  2015-06-11 09:34  DataGridViewSample\DataGridViewSample\bin\Debug\DataGridViewSample.vshost.exe
     文件         490  2013-06-18 20:28  DataGridViewSample\DataGridViewSample\bin\Debug\DataGridViewSample.vshost.exe.manifest
     文件        4004  2015-05-26 15:43  DataGridViewSample\DataGridViewSample\DataGridViewSample.csproj
     文件        2332  2015-05-27 07:39  DataGridViewSample\DataGridViewSample\Form1.cs
     文件        5403  2015-05-26 15:43  DataGridViewSample\DataGridViewSample\Form1.Designer.cs
     文件        5817  2015-05-26 15:43  DataGridViewSample\DataGridViewSample\Form1.resx
     文件        1698  2015-05-26 15:43  DataGridViewSample\DataGridViewSample\frmAdd.cs
     文件        7009  2015-05-26 15:43  DataGridViewSample\DataGridViewSample\frmAdd.Designer.cs
     文件        5817  2015-05-26 15:43  DataGridViewSample\DataGridViewSample\frmAdd.resx
     目录           0  2015-05-26 15:43  DataGridViewSample\DataGridViewSample\obj\
     目录           0  2015-05-26 15:43  DataGridViewSample\DataGridViewSample\obj\x86\
     目录           0  2015-05-27 07:39  DataGridViewSample\DataGridViewSample\obj\x86\Debug\
     文件        2108  2015-06-11 09:34  DataGridViewSample\DataGridViewSample\obj\x86\Debug\DataGridViewSample.csproj.FileListAbsolute.txt
     文件        1036  2015-05-26 15:44  DataGridViewSample\DataGridViewSample\obj\x86\Debug\DataGridViewSample.csproj.GenerateResource.Cache
     文件        9180  2015-05-26 15:44  DataGridViewSample\DataGridViewSample\obj\x86\Debug\DataGridViewSample.csprojResolveAssemblyReference.cache
     文件       14848  2015-05-27 07:39  DataGridViewSample\DataGridViewSample\obj\x86\Debug\DataGridViewSample.exe
     文件         180  2015-05-26 15:43  DataGridViewSample\DataGridViewSample\obj\x86\Debug\DataGridViewSample.Form1.resources
     文件         180  2015-05-26 15:43  DataGridViewSample\DataGridViewSample\obj\x86\Debug\DataGridViewSample.frmAdd.resources
     文件       40448  2015-05-27 07:39  DataGridViewSample\DataGridViewSample\obj\x86\Debug\DataGridViewSample.pdb
     文件         180  2015-05-26 15:44  DataGridViewSample\DataGridViewSample\obj\x86\Debug\DataGridViewSample.Properties.Resources.resources
     文件        2873  2015-05-26 15:44  DataGridViewSample\DataGridViewSample\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        7122  2015-06-11 09:32  DataGridViewSample\DataGridViewSample\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     目录           0  2014-05-02 12:16  DataGridViewSample\DataGridViewSample\obj\x86\Debug\TempPE\
............此处省略8个文件信息

评论

共有 条评论