linq to sql数据绑定到combobox [英] linq to sql data bindings to combobox
本文介绍了linq to sql数据绑定到combobox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的朋友们,
请查看以下linq代码,
Dear Friends,
please look at following linq code,
using (var context = new ERPHS.Classes.CustomerSupplier.dbDataContext())
{
var data= from v in context.Parties orderby v.isCustomer descending select new { v.Mobile, Name = v.Name + " " + v.Company + " " + ((v.isCustomer == true) ? "Customer" : (v.isCustomer == false) ? "Supplier" : "") };
BindingSource bs = new BindingSource();
bs.DataSource = data;
PartyNamecombobox.DataSource=bs;
PartyNamecombobox.DisplayMember = "Name";
PartyNamecombobox.ValueMember = "Mobile";
}
价值变动时
我输入以下代码
at the event of value changed I typed following code
<pre lang="c#"> private void PartyNamecombobox_SelectedValueChanged(object sender, EventArgs e)
{
var value = (string)PartyNamecombobox .SelectedValue;
cbAdvanceBooking.Checked = false;
#region Decide whether advance booking should be granted or not
if (value == "1")
{
cbAdvanceBooking.Visible = false;
}
else
{
cbAdvanceBooking.Visible = true;
}
#endregion
}
粗线给我错误,
the bold line gives me the error,
An unhandled exception of type 'System.InvalidCastException' occurred in ERPHS.exe
Additional information: Unable to cast object of type '<>f__AnonymousType2`2[System.String,System.String]' to type 'System.String'.
你能让我知道删除这个错误的最好的事情,因为在调试我可以看到name和id作为combbox的selectedvalue属性的值,
can you let me know the best thing to remove this error as in the debug I can see both name and id as the value of selectedvalue property of combbox,
推荐答案
查看此博客
http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/15cc4a42-6b04-4705-965f-f6b2af2c09b2 [ ^ ]
--NDK
check this blog
http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/15cc4a42-6b04-4705-965f-f6b2af2c09b2[^]
--NDK
为我工作的解决方案是在设置数据源值之前删除处理程序并添加设置数据源值
PartyNamecombobox.SelectedIndexChanged - = new EventHandler(PartyNamecombobox_SelectedValueChanged);
BindingSource bs = new BindingSource();
bs.DataSource = data;
PartyNamecombobox.DataSource = bs;
PartyNamecombobox.DisplayMember =Name;
PartyNamecombobox.ValueMember =Mobile;
PartyNamecombobox.SelectedIndexChanged + = new EventHandler(PartyNamecombobox_SelectedValueChanged);
Solution working for me is remove handler before setting datasource value and add setting datasource value
PartyNamecombobox.SelectedIndexChanged -=new EventHandler(PartyNamecombobox_SelectedValueChanged);
BindingSource bs = new BindingSource();
bs.DataSource = data;
PartyNamecombobox.DataSource=bs;
PartyNamecombobox.DisplayMember = "Name";
PartyNamecombobox.ValueMember = "Mobile";
PartyNamecombobox.SelectedIndexChanged +=new EventHandler(PartyNamecombobox_SelectedValueChanged);
这篇关于linq to sql数据绑定到combobox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文