private void button1_Click(object sender, EventArgs e)
{
////这段可正常运行
//comboBox1.DisplayMember = "CD";
//comboBox1.ValueMember = "CD";
//comboBox1.DataSource = ftTestClass.GetDataTable_Type();
////comboBox1.SelectedIndex = 2;
//comboBox1.SelectedValue = "BB"; //这段出错
using (ComboBox cmb = new ComboBox())
{
//cmb.Parent = this;
cmb.ValueMember = "CD"; //Code
cmb.DisplayMember = "CD"; //Code
cmb.DataSource = ftTestClass.GetDataTable_Type(); //{AA, BB, CC, DD, EE}
//cmb.SelectedIndex = 2;
cmb.SelectedValue = "BB";
Console.WriteLine(cmb.SelectedValue.ToString()); //出错,因为 SelectedValue=null,明明付了值呀,付不上去
}
}
{
////这段可正常运行
//comboBox1.DisplayMember = "CD";
//comboBox1.ValueMember = "CD";
//comboBox1.DataSource = ftTestClass.GetDataTable_Type();
////comboBox1.SelectedIndex = 2;
//comboBox1.SelectedValue = "BB"; //这段出错
using (ComboBox cmb = new ComboBox())
{
//cmb.Parent = this;
cmb.ValueMember = "CD"; //Code
cmb.DisplayMember = "CD"; //Code
cmb.DataSource = ftTestClass.GetDataTable_Type(); //{AA, BB, CC, DD, EE}
//cmb.SelectedIndex = 2;
cmb.SelectedValue = "BB";
Console.WriteLine(cmb.SelectedValue.ToString()); //出错,因为 SelectedValue=null,明明付了值呀,付不上去
}
}
以后下面的代码不要写了,在页面上当前选择项是这个吗?
class Grade{
public string Text="";
public int =100;
public override string ToString(){
return Text;
}
}
在ComboBox中添加此类的对象就行了,想怎么玩都行
不是这样嘛
ComboBox cmb = new ComboBox();
cmb.Items.Add("a");
你那是什么赋值?
Combobox里面都是 items,每个item对应一个object
就可以了。
不明白你要怎么操作
ComboBox ,ComboBox ,就是组合box,赋值只能对这个组合里面的单个对象 来操作啊。
public static DataTable GetDataTable_Type()
{
DataTable dt = new DataTable();
dt.Columns.Add("CD", typeof(string));
dt.Columns.Add("DS", typeof(string));
dt.Columns.Add("CDDS", typeof(string));
dt.LoadDataRow(new string[] { "AA", "aaa", "AA - aaa" }, true);
dt.LoadDataRow(new string[] { "BB", "bbb", "BB - bbb" }, true);
dt.LoadDataRow(new string[] { "CC", "ccc", "CC - ccc" }, true);
dt.LoadDataRow(new string[] { "DD", "ddd", "DD - ddd" }, true);
dt.LoadDataRow(new string[] { "EE", "eee", "EE - eee" }, true);
return dt;
}private void button1_Click(object sender, EventArgs e)
{
//这段出错
using (ComboBox cmb = new ComboBox())
{
//cmb.Parent = this;
cmb.ValueMember = "CD"; //Code
cmb.DisplayMember = "CD"; //Code
cmb.DataSource = this.GetDataTable_Type(); //{AA, BB, CC, DD, EE}
//cmb.SelectedIndex = 2;
cmb.SelectedValue = "CC";
Console.WriteLine(cmb.SelectedValue.ToString()); //出错,因为 SelectedValue=null,明明付了值呀,付不上去
}
}
付值不会提示出错,但在调试器中看到其值仍为null,所以不能ToString。
(调试时把 上面代码中的 public static DataTable GetDataTable_Type()的“static”去掉,笔误)
你不管赋什么值都会变成null;你是想设置cmb的默认选项吧
只能用selectIndex来设定
因为DataGridViewComboBoxColumn不好看,
但自己写的付不了值。
public DataTable GetDataTable_Type()
{
DataTable dt = new DataTable();
dt.Columns.Add("CD", typeof(string));
dt.Columns.Add("DS", typeof(string));
dt.Columns.Add("CDDS", typeof(string), "CD+' - '+DS");
dt.LoadDataRow(new string[] { "AA", "aaa" }, true);
dt.LoadDataRow(new string[] { "BB", "bbb" }, true);
dt.LoadDataRow(new string[] { "CC", "ccc" }, true);
dt.LoadDataRow(new string[] { "DD", "ddd" }, true);
dt.LoadDataRow(new string[] { "EE", "eee" }, true);
return dt;
}protected override void OnCellEnter(DataGridViewCellEventArgs e) //DataGridView event
{
//下拉列表
if (ftListColumns != null)
{
if (ftListColumns.Contains(this.CurrentCell.OwningColumn))
{
if (this.CurrentCell.OwningColumn.DataPropertyName == "TP")
{
ComboBox cmb = new ComboBox();
//cmb.Visible = false;
//cmb.Parent = this;
cmb.DropDownWidth = 200;
cmb.DropDownStyle = ComboBoxStyle.DropDownList;
DataTable dt = ftTestClass.GetDataTable_Type();
cmb.DisplayMember = "CDDS";
cmb.ValueMember = "CD";
cmb.DataSource = dt;
cmb.SelectedValue = this.CurrentCell.EditedFormattedValue; //这里付的是网格当前值,但付不上,仍是null
//cmb.value = this.CurrentCell.EditedFormattedValue;
//cmb.Text = this.CurrentCell.EditedFormattedValue.ToString();
cmb.SelectedValueChanged += new EventHandler(delegate(object sender2, EventArgs e2)
{
if (!this.CurrentCell.Value.Equals((sender2 as ComboBox).SelectedValue))
{
this.CurrentCell.Value = (sender2 as ComboBox).SelectedValue; //
}
});
//把combobox浮到DataGridView上面
{
Control ctl = cmb;
this.Controls.Add(ctl);
ctl.Bounds = this.GetCellDisplayRectangle(this.CurrentCell.ColumnIndex, this.CurrentCell.RowIndex, true);
ctl.LostFocus += new EventHandler(delegate(object sender, EventArgs e) { (sender as Control).Visible = false; });
ctl.Visible = true;
ctl.Focus();
ctl.BringToFront();
}
}
}
}
base.OnCellEnter(e);
}protected override void OnCellLeave(DataGridViewCellEventArgs e)
{
base.OnCellLeave(e); if (this.ftFloatControl != null)
{
this.ftFloatControl.Visible = false;
}
}
一直很烦combobox,不管是delphi、VB6还是C#中的都烦。
唯独delphi中的DbLookupCombobox很好用。
看 0楼 的代码就知道了。
可能是只读的属性吧,不能赋值
连Combobox控件都不会用,,
1. 设置了 ComboBox的Parent 属性
2. 或者 DataGridView1.Controls.Add(cmb)
就可以 付 SelectedValue 的值。难道保存 SelectedValue还要依lai4 其Parent?
{
}这句话是什么意思知道吗? 出了大括号ComboBox就会被释放掉的呢.....