Datagridview的datasource设为某datatable,然后如何将某列缺省TextBox改变成DataGridViewComboBoxColumn
我终于给你弄出来了。以显示人员性别为例,例子做法如下:
第一步:在Form里面建立两个DataGridView,分别命名为:dgvOrgin(原始的显示),dgvCombox(作为显示ComboBoxColumn的例子)
第二步:在Form的Load事件里面添加下面的代码:
List<string> LtSex = new List<string>() { string.Empty, "男", "女" };
DataGridViewComboBoxColumn ClmSex = new DataGridViewComboBoxColumn();
dgvCombox.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {ClmSex});
ClmSex.DataSource = LtSex;
ClmSex.DataPropertyName = "Sex";
ClmSex.HeaderText = "性别";
DataTable dtSex = new DataTable();
dtSex.TableName = "tb_Person";
DataColumn Dc1 = new DataColumn("性别", typeof(string));
Dc1.ColumnName = "Sex";
Dc1.Caption = "性别";
dtSex.Columns.Add(Dc1);
dtSex.Rows.Add("男");
dtSex.Rows.Add("男");
dtSex.Rows.Add("女");
dtSex.Rows.Add("男");
dtSex.Rows.Add("男");
dtSex.Rows.Add("女");
dtSex.Rows.Add("男");
dtSex.Rows.Add("女");
dgvCombox.DataSource = dtSex.DefaultView;
dgvOrgin.DataSource = dtSex.DefaultView;
最终的结果如下:
缺省是DataGridViewTextBoxColumn
怎么变成DataGridViewComboBoxColumn
textbox 想 改用为 combobox ???
comboBox1.DataSource = dt1;
comboBox1.ValueMember = "id";
comboBox1.DisplayMember = "name";
提交的时候获取combobox的值这样获取:
comboBox1.SelectedValue.ToString()