鸟语天空
DataGridView绑定DataTable
post by:追风剑情 2016-11-4 15:13
示例一: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 DataSetTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            //创建虚拟数据库
            DataSet dataSet = new DataSet();
            //创建数据表
            DataTable table = new DataTable("数据表");

            DataColumn column;
            DataRow row;

            //创建主键列
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Int32");
            column.ColumnName = "ID";
            column.Caption = "ID";
            column.ReadOnly = true;
            column.Unique = true;
            table.Columns.Add(column);

            //创建其它列
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "用户名";
            column.AutoIncrement = false;
            column.Caption = "用户名";
            column.ReadOnly = false;
            column.Unique = false;
            table.Columns.Add(column);

            //设置主键
            DataColumn[] PrimaryKeyColumns = new DataColumn[1];
            PrimaryKeyColumns[0] = table.Columns["ID"];
            table.PrimaryKey = PrimaryKeyColumns;

            //表添加入数据库
            dataSet.Tables.Add(table);

            //向表中插入数据
            for (int i = 0; i <= 5; i++)
            {
                row = table.NewRow();
                row["ID"] = i;
                row["用户名"] = "名字" + i;
                table.Rows.Add(row);
            }

            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.DataSource = table;
        }
    }
}

 

运行效果

111111.png

 

 

示例二:  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 DataSetTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            //创建虚拟数据库
            DataSet dataSet = new DataSet();
            //创建数据表
            DataTable table = new DataTable("数据表");

            DataColumn column;
            DataRow row;

            //创建主键列
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Int32");
            column.ColumnName = "ID";
            column.Caption = "ID";
            column.ReadOnly = true;
            column.Unique = true;
            table.Columns.Add(column);

            //创建其它列
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "UserName";
            column.AutoIncrement = false;
            column.Caption = "用户名";
            column.ReadOnly = false;
            column.Unique = false;
            table.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Boolean");
            column.ColumnName = "IsEnabled";
            column.AutoIncrement = false;
            column.Caption = "开启";
            column.ReadOnly = false;
            column.Unique = false;
            table.Columns.Add(column);

            //设置主键
            DataColumn[] PrimaryKeyColumns = new DataColumn[1];
            PrimaryKeyColumns[0] = table.Columns["ID"];
            table.PrimaryKey = PrimaryKeyColumns;

            //表添加入数据库
            dataSet.Tables.Add(table);

            //向表中插入数据
            for (int i = 0; i <= 5; i++)
            {
                row = table.NewRow();
                row["ID"] = i;
                row["UserName"] = "名字" + i;
                row["IsEnabled"] = true;
                table.Rows.Add(row);
            }

            //DataGridView的列与数据表中的列(ColumnName)绑定
            //这种情况适合于已经手动在DataGridView中创建了列,只想为这些列绑定数据。
            dataGridView1.Columns[0].DataPropertyName = "ID";
            dataGridView1.Columns[1].DataPropertyName = "UserName";
            dataGridView1.Columns[2].DataPropertyName = "IsEnabled";
            //禁止自动创建新列
            dataGridView1.AutoGenerateColumns = false;
            //禁止自动添加新行
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.DataSource = table;
        }
    }
}

效果

3333.png

运行

22222.png

 

 

 

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容