只剩70分全奉上。一个搞了好久没有搞定的问题。(快疯了)
我是在datagridview中增加一个DataGridViewComboBoxColumn,设某一列中显示一个下拉列表框以供选择数据。代码如下:
DataGridViewComboBoxColumn cbc = new DataGridViewComboBoxColumn();
cbc.DataSource = dt;
cbc.DisplayMember = "性别 ";
cbc.ValueMember = "性别 ";
cbc.DataPropertyName = "性别 ";
cbc.HeaderText = "性别 ";
dataGridView1.Columns.AddRange(cbc);
我只做到了这一步实现了显示一个下拉列表框。问题就是。。。。。
现在我不知道怎么样才能给列表框增加(男,女)选项,以供选择数据。
------解决方案--------------------另外再加一列sex,放男女字段,再itemdatabind中根据sex列处理列表.
------解决方案--------------------男女选项应该在cbc.DataSource = dt;的dt中先加好,就可以了
------解决方案--------------------DataGridViewComboBoxColumn 难道没有Item吗 ?
加两个Item 啊
------解决方案--------------------//combobox数据源。。。
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn( "Sex ", typeof(string)));
DataRow row = dt.NewRow();
row[0] = "man ";
dt.Rows.Add(row);
DataRow row2 = dt.NewRow();
row2[0] = "woman ";
dt.Rows.Add(row2);
dt.AcceptChanges();
//DataGridView列
DataGridViewComboBoxColumn cbc = new DataGridViewComboBoxColumn();
cbc.DataSource = dt;
cbc.DisplayMember = "Sex ";
cbc.ValueMember = "Sex ";
cbc.DataPropertyName = "Sex ";
cbc.HeaderText = "Sex ";
DataGridViewTextBoxColumn cid = new DataGridViewTextBoxColumn();
cid.HeaderText = "ID ";
cid.DataPropertyName= "ID ";
DataGridViewTextBoxColumn cname = new DataGridViewTextBoxColumn();
cname.HeaderText = "Name ";
cname.DataPropertyName = "Name ";
dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {cid, cname, cbc});
//DataGridView数据源
DataTable dtSource = new DataTable();
dtSource.Columns.Add( "ID ", typeof(Int32));
dtSource.Columns.Add( "Name ", typeof(string));
dtSource.Columns.Add( "Sex ", typeof(string));
DataRow r1 = dtSource.NewRow();
r1[0] = 1;
r1[1] = "test ";
dtSource.Rows.Add(r1);
dtSource.AcceptChanges();
dataGridView1.DataSource = dtSource;