我正在将组合框与数据源、显示成员、值成员绑定(bind)。它在我的电脑上工作正常,但在客户端电脑上不工作。以下是我的源代码:
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 = "";
}
}
}
这两天一直困扰着我,我几乎尝试了所有技巧,但仍然没有用。
客户的电脑使用的是 Windows 7 旗舰版、sql server 2005 和 .net framework 3.5。
最佳答案
如果在构造函数中调用 cbxAlloyBinding()
之前调用了 cbxMetal_SelectedIndexChanged
,这肯定会发生。
例如(参见下面的代码),您可能在构造函数中有其他组合框绑定(bind),它们可能位于构造函数中的 cbxAlloyBinding()
之前,并且这些绑定(bind)正在调用 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
之前;
请记住,System.DataRow.DataRowView
只有在
ComboBox.SelectedValue
is called beforeValueMember
assignment.
关于c# - 显示 system.data.datarowview 的组合框数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14620545/