资 源 简 介
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; 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来做导出都是可以的 } } 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; } }}