示例代码:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace WindowsApplication6
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- DataTable table = new DataTable();
- DataColumn c1 = new DataColumn("ID", typeof(int));
- table.Columns.Add(c1);
- DataColumn c2 = new DataColumn("name", typeof(string));
- table.Columns.Add(c2);
- DataRow r1 = table.NewRow();
- r1["ID"] = 1;
- r1["name"] = "abc";
- table.Rows.Add(r1);
- DataRow r2 = table.NewRow();
- r2["ID"] = 2;
- r2["name"] = "bcd";
- table.Rows.Add(r2);
- this.dataGridView1.DataSource = table;
- }
- //改变HeaderText
- private void button2_Click(object sender, EventArgs e)
- {
- this.dataGridView1.Columns["ID"].HeaderText = "第一列";
- this.dataGridView1.Columns["name"].HeaderText = "第而列";
- }
- }
- }