数据绑定组合框显示system.data.datarowview [英] Combobox databinding showing system.data.datarowview

查看:139
本文介绍了数据绑定组合框显示system.data.datarowview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绑定组合框与数据源,的DisplayMember,valuemember。它工作正常在我的电脑,但它不是在客户端PC的工作。下面是我的源代码:

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方法是从用户控件的构造函数叫做

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旗舰版,SQL Server 2005和.NET Framework 3.5的

Client's PC is using Windows 7 ultimate, sql server 2005 and .net framework 3.5.

推荐答案

这绝对如果发生你的 cbxMetal_SelectedIndexChanged 在<被称为code> cbxAlloyBinding()被称为在构造函数。

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆