我将组合框与数据源、显示成员、值成员绑定。它在我的电脑上运行良好,但在客户端电脑上无法运行。以下是我的源代码:
cbxAlloyBinding 方法是从 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 = "";
}
}
}
这两天一直困扰着我,我几乎尝试了所有技巧,但仍然不起作用。
客户的PC使用的是Windows 7 Ultimate、sql server 2005和.net Framework 3.5。
如果您的cbxMetal_SelectedIndexChanged
之前被调用cbxAlloyBinding()
在你的构造函数中调用。
例如(请参阅下面的代码),您可能在构造函数中具有其他组合框绑定,这些绑定可能位于之前cbxAlloyBinding()
在构造函数中,这些绑定正在调用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
and ValueMember
被分配;
只要记住,System.DataRow.DataRowView
仅当以下情况时才会发生
ComboBox.SelectedValue
之前被调用ValueMember
任务。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)