问题描述
我正在将组合框与数据源、显示成员、值成员绑定.它在我的电脑上工作正常,但在客户端电脑上却无法正常工作.以下是我的源代码:
I am binding combobox with datasource, displaymember, valuemember. It is working fine in my computer but it is not working in clients pc. Following is my source code:
cbxAlloyBinding 方法是从 UserControl 的构造函数调用的.
cbxAlloyBinding method is called from the Constructor of the UserControl.
private void cbxAlloyBinding()
{
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter("SELECT alloyName,alloyId FROM alloy", con);
adp.Fill(dt);
if (dt.Rows.Count > 0)
{
cbxMetal.DisplayMember = "alloyName";
cbxMetal.ValueMember = "alloyId";
cbxMetal.DataSource = dt;
}
else
{
cbxMetal.Text = "";
}
}
private void cbxMetal_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbxMetal.SelectedIndex != -1)
{
DataTable dt = new DataTable();
tempcmd = new SqlCommand("SELECT specification,alloyCode FROM alloy where alloyId='" + cbxMetal.SelectedValue + "'", con);
SqlDataAdapter adp = new SqlDataAdapter(tempcmd);
adp.Fill(dt);
if (dt.Rows.Count > 0)
{
txtSpecification.Text = dt.Rows[0]["alloyCode"].ToString();
txtSupplyConditions.Text = dt.Rows[0]["specification"].ToString();
cbxheatBinding();
}
else
{
txtSpecification.Text = "";
}
}
}
这两天一直困扰着我,我几乎尝试了所有技巧,但仍然无法正常工作.
This is bothering me from last two days and i almost tried all tricks but it is still not working.
客户的 PC 使用的是 Windows 7 Ultimate、sql server 2005 和 .net framework 3.5.
Client's PC is using Windows 7 ultimate, sql server 2005 and .net framework 3.5.
推荐答案
如果在构造函数中调用 cbxAlloyBinding()
之前调用 cbxMetal_SelectedIndexChanged
,这肯定会发生.
This definitely happens if your cbxMetal_SelectedIndexChanged
is called before cbxAlloyBinding()
is called in your constructor.
例如(请参阅下面的代码),您可能在构造函数中具有其他组合框绑定,它们可能位于构造函数中的 cbxAlloyBinding()
之前,并且这些绑定正在调用 cbxMetal_SelectedIndexChanged
.
For instance (see the code below), you may have other combobox bindings in constructor which may come before cbxAlloyBinding()
in constructor, and those bindings are calling cbxMetal_SelectedIndexChanged
.
public Constructor()
{
InitializeComponent();
cbxheatBinding(); //1st Three Binding Methods may be somehow related to your cbxMetal,
dtpStartDateBinding(); //which leads them to call cbxMetal_SelectedIndexChanged method.
dtpEndDateBinding();
cbxAlloyBinding();
}
我怀疑您的 cbxMetal.DataSource
是从代码中的某个其他点设置的,并且早在 DisplayMember
和 ValueMember
被分配之前;
What I suspect is your cbxMetal.DataSource
is set from some other point in your code and well before DisplayMember
and ValueMember
are assigned;
请记住,System.DataRow.DataRowView
仅在
ComboBox.SelectedValue
在 ValueMember
赋值之前被调用.
这篇关于显示 system.data.datarowview 的组合框数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!