dataset的表中有一列“姓名”,请问怎么用代码把它绑定成ComboBoxColumn。下面代码运行后新增了一列,内容是dataset表中“姓名”的值,我想要的是把“姓名”一列改为下拉列表形式,Items值为:A、B、C。代码该如何改呢?dadapter = new SqlDataAdapter();
dadapter.SelectCommand = new SqlCommand(selectString, myConn);
dset = new DataSet();
dadapter.Fill(dset, "UserTable");
builder = new SqlCommandBuilder(dadapter);
bindingSource2 = new BindingSource();
bindingSource2.DataSource = dset;
bindingSource2.DataMember = "UserTable";
this.dataGridView1.DataSource = bindingSource2;DataGridViewComboBoxColumn abc = new DataGridViewComboBoxColumn();
abc.DataSource = dset.Tables["UserTable"].DefaultView;
abc.DisplayMember = "姓名";
abc.ValueMember = "姓名";
dataGridView1.Columns.Insert(0, abc);
dadapter.SelectCommand = new SqlCommand(selectString, myConn);
dset = new DataSet();
dadapter.Fill(dset, "UserTable");
builder = new SqlCommandBuilder(dadapter);
bindingSource2 = new BindingSource();
bindingSource2.DataSource = dset;
bindingSource2.DataMember = "UserTable";
this.dataGridView1.DataSource = bindingSource2;DataGridViewComboBoxColumn abc = new DataGridViewComboBoxColumn();
abc.DataSource = dset.Tables["UserTable"].DefaultView;
abc.DisplayMember = "姓名";
abc.ValueMember = "姓名";
dataGridView1.Columns.Insert(0, abc);
DataGridViewComboBoxColumn abc = new DataGridViewComboBoxColumn();
abc.DataSource = dset.Tables["UserTable"].DefaultView;
abc.DataPropertyName = "姓名";
abc.HeaderText = "姓名";
abc.Items.AddRange(new object[] {
"A",
"B",
"C"
});
DataGridViewComboBoxColumn的DataPropertyName的属性指定的是你的DataGrid中DataSource的列名,并非DataGridViewComboBoxColumn自己本身的Datasource的列名...你再试试
System.Windows.Forms.DataGridViewComboBoxColumn col1 = new DataGridViewComboBoxColumn(); //定义一个新列
col1.DataSource = this.dataSet11.Tables["sms_category"]; //为该列指定数据源
col1.DisplayMember = "category_desc"; //指定显示成员
col1.ValueMember = "category_id"; //指定值成员
col1.HeaderText = "类别"; //列头标题
col1.DataPropertyName = "category_id"; //指定该列加到的dataGridView1中的对应列的名字this.dataGridView1.Columns.Add(col1); //将该列加入dataGridView1.Columns中在我这里可以...
col1.DataSource = this.dset.Tables["UserTable"]; //为该列指定数据源
col1.DisplayMember = "姓名"; //指定显示成员
col1.ValueMember = "姓名"; //指定值成员
col1.HeaderText = "姓名"; //列头标题
col1.DataPropertyName = "姓名"; //指定该列加到的dataGridView1中的对应列的名字
col1.Items.AddRange(new object[] {"a","b","c"});
this.dataGridView1.Columns.Add(col1); //将该列加入dataGridView1.Columns中
多谢longcanhua朋友,我把上面的代码加入后,运行显示多了一个“姓名”,原来的也还在,绑定的不是原来table中的姓名,而是新增了一列,还是col1.Items.AddRange(new object[] {"a","b","c"}); 这一条还是有错误,再麻烦帮我看看!
http://www.microsoft.com/china/msdn/library/langtool/vsdotnet/vs05a9.mspx
//删除自动生成的 姓名 列。
dataGridView1.Columns.Remove("姓名");
System.Windows.Forms.DataGridViewComboBoxColumn List = new DataGridViewComboBoxColumn();
List.DisplayIndex = 0;
List.HeaderText = "姓名";
List.DataPropertyName = "姓名";
List.DataSource = this.dset.Tables["UserTable"];
List.DisplayMember = "姓名";
List.ValueMember = "姓名";
/* List.Items.AddRange(new object[] {"a","b","c"}); */
dataGridView1.Columns.Add(List);