c# - 显示 system.data.datarowview 的组合框数据绑定(bind)

标签 c# sql-server data-binding combobox

我正在将组合框与数据源、显示成员、值成员绑定(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 是从你代码中的其他点设置的,并且在分配 DisplayMemberValueMember 之前;

请记住,System.DataRow.DataRowView 只有在

ComboBox.SelectedValue is called before ValueMember assignment.

关于c# - 显示 system.data.datarowview 的组合框数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14620545/

相关文章:

c# - 错误 CS1002 : ; expected & (31, 13):错误 CS1525:无效的表达式项 'else'

c# - 如何循环目录并仅通过使用 C# 的 Web 方法返回文件夹名称?

c# - 将 Asp.net 核心 Controller 限制为特定域名

sql - 在 SQL Server 上插入更新存储过程

wpf - 如何调试 Windows 运行时数据绑定(bind)?

javascript - 如何在单击按钮时获取对象的 id 和名称并将其显示在 ASP.NET View 的模式中

sql-server - 无法使用 Go 连接到 MS SQL Server

SQL Server : Insert if doesn't exist, 否则更新并插入到另一个表中

数据网格中的 Wpf 绑定(bind)错误仍然显示值

在没有 InvokeRequired 的多线程场景中,Winforms 数据绑定(bind)到业务对象?