在這裏,我失去了我的感覺......ComboBox.SelectedValue綁定到一個列表
後搞砸了我的組合框的形式與一個屬性改變,排序=真。
這個屬性搞亂了SelectedValue,我想知道爲什麼。
看看代碼(你可以將其粘貼到新的WinForms項目,並添加一個組合框後,它會工作):
private void Form1_Load(object sender, EventArgs e)
{
List<ColumnFilter> ColumnFilters = new List<ColumnFilter>();
ColumnFilters.Add(new ColumnFilter("Ope_OpeID", "Ope_OpeID", ColumnFilterType.DataGridViewHidden));
ColumnFilters.Add(new ColumnFilter("Ope_Kod", "Ope_Kod", ColumnFilterType.DataGridViewHidden));
ColumnFilters.Add(new ColumnFilter("Ope_Imie", "Ope_Imie", ColumnFilterType.DataGridViewHidden));
ColumnFilters.Add(new ColumnFilter("Ope_Nazwisko", "Ope_Nazwisko", ColumnFilterType.DataGridViewHidden));
comboBox1.DataSource = ColumnFilters;
comboBox1.DisplayMember = "Description";
comboBox1.ValueMember = "Expression";
}
private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
MessageBox.Show(comboBox1.SelectedValue.ToString());
}
}
public enum ColumnFilterType
{
DataGridView = 1,
DataGridViewHidden = 2,
DataTable = 3
}
public struct ColumnFilter
{
private string description;
private string expression;
public string Description
{
get { return description; }
set { description = value; }
}
public string Expression
{
get { return expression; }
set { expression = value; }
}
public readonly ColumnFilterType Type;
public ColumnFilter(string description, string expression, ColumnFilterType type)
{
this.description = description;
this.expression = expression;
this.Type = type;
}
public override string ToString()
{
return description;
}
}
正如你可以看到我manualy加4層結構到列表中,在SelectionChangeCommited事件中,我想顯示選定結構的當前值。
所以,當我選擇Ope_OpeID我想看到一個值Ope_OpeID,當我選擇Ope_Imie我想看到值Ope_Imie。換句話說,值必須等於顯示的文本。
問題是,當我例如選擇Ope_Imie,那麼值是Ope_OpeID! 當我選擇Ope_OpeID時,值是Ope_Nazwisko。我不知道爲什麼這樣工作。
的「搞笑」(不那麼好笑2小時生活的浪費..)之後的事情是,當我添加到列表中不同項目集合:
ColumnFilters.Add(new ColumnFilter("1Ope_OpeID", "1Ope_OpeID", ColumnFilterType.DataGridViewHidden));
ColumnFilters.Add(new ColumnFilter("2Ope_Kod", "2Ope_Kod", ColumnFilterType.DataGridViewHidden));
ColumnFilters.Add(new ColumnFilter("3Ope_Imie", "3Ope_Imie", ColumnFilterType.DataGridViewHidden));
ColumnFilters.Add(new ColumnFilter("4Ope_Nazwisko", "4Ope_Nazwisko", ColumnFilterType.DataGridViewHidden));
它可以作爲之嫌。所以,排序有些問題,它只排序第一個字符?
謝謝你的時間。
常見的傢伙:)這是基本的功能,我需要你的幫助...: – Wodzu 2010-07-22 08:03:54