DataGridView绑定DataTable

作者:追风剑情 发布于:2016-11-4 15:13 分类:C#

示例一:DataGridView自动创建列,并绑定数据
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace DataSetTest
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. Init();
  18. }
  19.  
  20. private void Init()
  21. {
  22. //创建虚拟数据库
  23. DataSet dataSet = new DataSet();
  24. //创建数据表
  25. DataTable table = new DataTable("数据表");
  26.  
  27. DataColumn column;
  28. DataRow row;
  29.  
  30. //创建主键列
  31. column = new DataColumn();
  32. column.DataType = System.Type.GetType("System.Int32");
  33. column.ColumnName = "ID";
  34. column.Caption = "ID";
  35. column.ReadOnly = true;
  36. column.Unique = true;
  37. table.Columns.Add(column);
  38.  
  39. //创建其它列
  40. column = new DataColumn();
  41. column.DataType = System.Type.GetType("System.String");
  42. column.ColumnName = "用户名";
  43. column.AutoIncrement = false;
  44. column.Caption = "用户名";
  45. column.ReadOnly = false;
  46. column.Unique = false;
  47. table.Columns.Add(column);
  48.  
  49. //设置主键
  50. DataColumn[] PrimaryKeyColumns = new DataColumn[1];
  51. PrimaryKeyColumns[0] = table.Columns["ID"];
  52. table.PrimaryKey = PrimaryKeyColumns;
  53.  
  54. //表添加入数据库
  55. dataSet.Tables.Add(table);
  56.  
  57. //向表中插入数据
  58. for (int i = 0; i <= 5; i++)
  59. {
  60. row = table.NewRow();
  61. row["ID"] = i;
  62. row["用户名"] = "名字" + i;
  63. table.Rows.Add(row);
  64. }
  65.  
  66. dataGridView1.AllowUserToAddRows = false;
  67. dataGridView1.DataSource = table;
  68. }
  69. }
  70. }

 

运行效果

111111.png

 

 

示例二:  DataGridView先手动创建好列,再绑定数据

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace DataSetTest
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. Init();
  18. }
  19.  
  20. private void Init()
  21. {
  22. //创建虚拟数据库
  23. DataSet dataSet = new DataSet();
  24. //创建数据表
  25. DataTable table = new DataTable("数据表");
  26.  
  27. DataColumn column;
  28. DataRow row;
  29.  
  30. //创建主键列
  31. column = new DataColumn();
  32. column.DataType = System.Type.GetType("System.Int32");
  33. column.ColumnName = "ID";
  34. column.Caption = "ID";
  35. column.ReadOnly = true;
  36. column.Unique = true;
  37. table.Columns.Add(column);
  38.  
  39. //创建其它列
  40. column = new DataColumn();
  41. column.DataType = System.Type.GetType("System.String");
  42. column.ColumnName = "UserName";
  43. column.AutoIncrement = false;
  44. column.Caption = "用户名";
  45. column.ReadOnly = false;
  46. column.Unique = false;
  47. table.Columns.Add(column);
  48.  
  49. column = new DataColumn();
  50. column.DataType = System.Type.GetType("System.Boolean");
  51. column.ColumnName = "IsEnabled";
  52. column.AutoIncrement = false;
  53. column.Caption = "开启";
  54. column.ReadOnly = false;
  55. column.Unique = false;
  56. table.Columns.Add(column);
  57.  
  58. //设置主键
  59. DataColumn[] PrimaryKeyColumns = new DataColumn[1];
  60. PrimaryKeyColumns[0] = table.Columns["ID"];
  61. table.PrimaryKey = PrimaryKeyColumns;
  62.  
  63. //表添加入数据库
  64. dataSet.Tables.Add(table);
  65.  
  66. //向表中插入数据
  67. for (int i = 0; i <= 5; i++)
  68. {
  69. row = table.NewRow();
  70. row["ID"] = i;
  71. row["UserName"] = "名字" + i;
  72. row["IsEnabled"] = true;
  73. table.Rows.Add(row);
  74. }
  75.  
  76. //DataGridView的列与数据表中的列(ColumnName)绑定
  77. //这种情况适合于已经手动在DataGridView中创建了列,只想为这些列绑定数据。
  78. dataGridView1.Columns[0].DataPropertyName = "ID";
  79. dataGridView1.Columns[1].DataPropertyName = "UserName";
  80. dataGridView1.Columns[2].DataPropertyName = "IsEnabled";
  81. //禁止自动创建新列
  82. dataGridView1.AutoGenerateColumns = false;
  83. //禁止自动添加新行
  84. dataGridView1.AllowUserToAddRows = false;
  85. dataGridView1.DataSource = table;
  86. }
  87. }
  88. }

效果

3333.png

运行

22222.png

 

 

 

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号