DataGridView动态绑定combox列,checkbox列(仿照老大的代码自己写的一个小例子供自己以后参考)
首先在后台代码定义各种datagridview类型列并初始化;
然后将定义好的各列添加到datagridview的column集合中;
并设置各列数据源属性的名称或与 DataGridViewColumn 绑定的数据库列的名称(combox,checkbox就会默认绑定)
这里的Student类的属性必须都通过get;set取得和设置值。
Student类定义如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class Student { private string _id; public string Stu_ID { get { return _id; } set { _id = value; } } private string _name; public string Stu_Name { get { return _name; } set { _name = value; } } private int _age; public int Stu_Age { get { return _age; } set { _age = value; } } private string _sex; public string Stu_Sex { get { return _sex; } set { _sex = value; } } private int _isgraduate; public int Stu_IsGraduate { get { return _isgraduate; } set { _isgraduate = value; } } }
下面是Datagridview绑定combox,checkbox的代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 Dict { public partial class DataGridviewExp2 : Form { /// <summary> /// 此处定义并初始化DataGridView列 /// </summary> private DataGridViewTextBoxColumn userIdDataGridViewTextBoxColum = new DataGridViewTextBoxColumn(); private DataGridViewTextBoxColumn userNameDataGridViewTextBoxColum = new DataGridViewTextBoxColumn(); private DataGridViewTextBoxColumn userAgeDataGridViewTextBoxColum = new DataGridViewTextBoxColumn(); private DataGridViewComboBoxColumn userSexDataGridViewComboBoxColum = new DataGridViewComboBoxColumn(); private DataGridViewCheckBoxColumn userIsGraduateDataGridViewCheckBoxColum = new DataGridViewCheckBoxColumn(); public DataGridviewExp2() { InitializeComponent(); ///我放了两个BindingSource用来做数据源,此方法是BindingSource给数据源绑定数据 SetDataSourceBind(); ///将定义好的各个DataGridView列添加到DataGridView中,并设置详细信息 SetDataGridView(); DgvBind();///给DataGridView绑定数据源 } public void DgvBind() { //DGV.DataSource = StudentbindingSource; string sql = "select * from TB_Student"; Student stu = new Student(); DGV.DataSource = DALStudent.GetAll(stu, sql); ///GetAll方法放回的是一个List<object>,是通过反射得到对象,并对每一个对象赋值,然后将对象加到List中. } public void SetDataSourceBind() { ///我在设计窗口里放了两个BindingSource string sql = "Select DISTINCT(Stu_Sex) from TB_Student"; Student stu = new Student(); sexbindingSource.DataSource = DALStudent.GetAll(stu,sql); ///BindingSource一个起名为sexbindingSource(性别数据源),专门为性别combox列做数据源 sql = "select * from TB_Student"; StudentbindingSource.DataSource = DALStudent.GetAll(stu, sql); ///BindingSource另一个起名为StudentbindingSource(作为DataGridview的数据源) } /// <summary> /// 将先前动态定义的各个DatagridView列加入到DataGridview中,并进行详细设置 /// </summary> public void SetDataGridView() { DGV.Columns.AddRange(new DataGridViewColumn[] { userIdDataGridViewTextBoxColum, userNameDataGridViewTextBoxColum, userAgeDataGridViewTextBoxColum, userSexDataGridViewComboBoxColum, userIsGraduateDataGridViewCheckBoxColum }); userIdDataGridViewTextBoxColum.DataPropertyName = "Stu_ID";//获取或设置数据源属性的名称或与 DataGridViewColumn 绑定的数据库列的名称。 (继承自 DataGridViewColumn。) userIdDataGridViewTextBoxColum.HeaderText = "ID";//标题文本 userIdDataGridViewTextBoxColum.ToolTipText = "学生ID";//获取或设置提示文本 userIdDataGridViewTextBoxColum.Width = 60;//获取或设置列标题单元格的标题文本。 (继承自 DataGridViewColumn。) userIdDataGridViewTextBoxColum.Name = "userID";//获取或设置列名称。 (继承自 DataGridViewColumn。) userNameDataGridViewTextBoxColum.DataPropertyName = "Stu_Name"; userNameDataGridViewTextBoxColum.HeaderText = "姓名"; userNameDataGridViewTextBoxColum.Width = 60; userNameDataGridViewTextBoxColum.Name = "userName"; userAgeDataGridViewTextBoxColum.DataPropertyName = "Stu_Age"; userAgeDataGridViewTextBoxColum.HeaderText = "年龄"; userAgeDataGridViewTextBoxColum.Width = 60; userAgeDataGridViewTextBoxColum.Name = "Age"; userSexDataGridViewComboBoxColum.DataPropertyName = "Stu_Sex"; userSexDataGridViewComboBoxColum.Name = "Sex"; userSexDataGridViewComboBoxColum.HeaderText = "性别"; userSexDataGridViewComboBoxColum.DataSource = sexbindingSource;///数据源 userSexDataGridViewComboBoxColum.DisplayMember = "Stu_Sex";//显示的成员 userSexDataGridViewComboBoxColum.ValueMember = "Stu_Sex";///值成员 userIsGraduateDataGridViewCheckBoxColum.FalseValue = 0;///设置该列没选中是0,选中是1 userIsGraduateDataGridViewCheckBoxColum.TrueValue = 1; userIsGraduateDataGridViewCheckBoxColum.DataPropertyName = "Stu_IsGraduate"; userIsGraduateDataGridViewCheckBoxColum.HeaderText = "是否毕业"; userIsGraduateDataGridViewCheckBoxColum.Name = "IsGraduate"; } } }
通过上面将数据库查到的集合绑定到DataGridview中。
以下是数据库中表的信息:
代码运行后显示如下:
combox列和checkbox列是自动绑定的,并不是通过遍历Datagridview循环访问设置它的值。