C# 显示 system.data.datarowview 的组合框数据绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14620545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:27:02  来源:igfitidea点击:

Combobox databinding showing system.data.datarowview

c#sql-serverdata-bindingcombobox

提问by Saral Doshi

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 method is called from the Constructor of the UserControl.

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 = "";
            }

        }
    }

This is bothering me from last two days and i almost tried all tricks but it is still not working.

过去两天这一直困扰着我,我几乎尝试了所有技巧,但仍然无法正常工作。

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

客户的PC 使用Windows 7 Ultimate、sql server 2005 和.net framework 3.5。

回答by Fredrick Gauss

It seems problem is not with the code you pasted here, it may be with client environment, connection, privileges etc. You must give more info about that 'it is not working in clients pc'. What systems they use, what is the error, have you tried debugging in client side?

似乎问题不在于您在此处粘贴的代码,而可能在于客户端环境、连接、权限等。您必须提供有关“它在客户端 pc 中不起作用”的更多信息。他们使用什么系统,错误是什么,您是否尝试过在客户端调试?

*Edit:*then you have to find the problem tracing all the operation from the beginning. My advice is that make a dummy windows form application, just a standard form, a combobox and a button. ON the click event of button just do what you did. JUst bind the combo and send this temp app to the client. If it works then do step by step.Ex:

*编辑:*然后你必须从头开始追踪所有操作来找到问题。我的建议是制作一个虚拟的 windows 窗体应用程序,只是一个标准窗体、一个组合框和一个按钮。在按钮的点击事件上做你所做的。只需绑定组合并将此临时应用程序发送给客户端。如果它有效,那么一步一步地做。例如:

private void button1_Click(object sender, EventArgs e)
    {
        string conStr = "Data Source=PC-303\SQLEXPRESS;Initial Catalog=sokaklar;User ID=sa;Password=*****";

        SqlDataAdapter adapter = new SqlDataAdapter("SELECT DISTINCT IL, IL_ID FROM sokaklar ORDER BY IL", new SqlConnection(conStr));
        DataTable dt = new System.Data.DataTable();
        adapter.Fill(dt);            

        comboBox1.DisplayMember = "IL";
        comboBox1.ValueMember = "IL_ID";
        comboBox1.DataSource = dt;            
    }

I created this app in one minute, and it is working for me.

我在一分钟内创建了这个应用程序,它对我有用。

回答by Raj

"comboBox1.SelectedValue" returns object and you want as string so convert it Convert.ToString(comboBox1.SelectedValue) and then use

“comboBox1.SelectedValue”返回对象,您想要作为字符串,因此将其转换为 Convert.ToString(comboBox1.SelectedValue) 然后使用

for example:

例如:

  tempcmd = new SqlCommand("SELECT specification,alloyCode FROM alloy where alloyId='" + Convert.ToString(cbxMetal.SelectedValue) + "'", con);

回答by Marshal

This definitely happens if your cbxMetal_SelectedIndexChangedis called before cbxAlloyBinding()is called in your constructor.

如果在您的构造函数中cbxMetal_SelectedIndexChanged调用之前cbxAlloyBinding()调用您,则肯定会发生这种情况。

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.

例如(请参阅下面的代码),您可能在构造函数中有其他组合框绑定,它们可能出现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();
}

What I suspect is your cbxMetal.DataSourceis set from some other point in your code and well before DisplayMemberand ValueMemberare assigned;

我怀疑是你cbxMetal.DataSource是从你的代码中的一些其他的点集和以及之前DisplayMemberValueMember分配;

Just remember, System.DataRow.DataRowViewwill occur only if

请记住,System.DataRow.DataRowView只有在以下情况下才会发生

ComboBox.SelectedValueis called before ValueMemberassignment.

ComboBox.SelectedValueValueMember赋值之前调用。

回答by Magnus Alexander

I resolved same this:

我解决了同样的问题:

/*First get DataSource*/
comboBox1.DataSource = dt;
/*Then determine DisplayMember y ValueMember*/ 
comboBox1.DisplayMember = "YOUR_FIELD_NAME";
comboBox1.ValueMember = "YOUR_OTHER_FIELD_NAME";

This only works with System.Data.DataTable or List data sources

这仅适用于 System.Data.DataTable 或 List 数据源

回答by philx_x

setting the DisplayMemberand ValueMemeberafter setting the DataSourcefixed this issue for me.

设置DisplayMemberValueMemeber设置后DataSource为我修复了这个问题。

cbxMetal.DataSource = dt;
cbxMetal.DisplayMember = "alloyName";
cbxMetal.ValueMember = "alloyId";

回答by SOS21

It was showing me the same exception, so I did this and it worked

它向我展示了同样的异常,所以我做了这个并且它起作用了

    private void cb_category_SelectedIndexChanged(object sender, EventArgs e)
    {


            DataTable mydt = new DataTable();
            try
            {
                mydt = request.GetItem(int.Parse(cb_category.SelectedValue.ToString()));
            }
            catch { }

            if(mydt.Rows.Count>0)
            {
            cb_Item.DataSource = mydt;
            cb_Item.DisplayMember = "dispmember";
            cb_Item.ValueMember = "valmember";
            }
            else
            {
                cb_Item.DataSource = null;
            }       

    }

回答by Dhav

THIS WILL DEFINITELY HELP TO YOU

这肯定会对您有所帮助

on the load Event you want to Just Write this code

在你想要的加载事件上只写这个代码

onformload()
{
    cmb_dept.Items.Clear();
    SqlConnection conn = new SqlConnection(@"DATA SOURCE=(localdb)\MSSQLLocalDB;INTEGRATED SECURITY=true;INITIAL CATALOG=EMPLOYEE;");
    conn.Open();
    SqlCommand command = new SqlCommand("select dept_id, dept_name from department", conn);
    SqlDataAdapter adapter = new SqlDataAdapter(command);
    DataSet ds = new DataSet();
    adapter.Fill(ds);
    cmb_dept.ValueMember = "dept_id";
    cmb_dept.DisplayMember = "dept_name";
    cmb_dept.DataSource = ds.Tables[0];
}

try using Use the code where you want to access the values........

尝试使用 Use the code where you want to access the values........

string dept = cmb_dept.Text;
MessageBox.Show("val=" + dept);

YOUR combobox.text = System.Data.DataRowView Will be Solved ##

您的 combobox.text = System.Data.DataRowView 将被解决 ##