Combobox databinding showing system.data.datarowview

Deadly 提交于 2020-01-10 14:52:06

问题


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.

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.


回答1:


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

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();
}

What I suspect is your cbxMetal.DataSource is set from some other point in your code and well before DisplayMember and ValueMember are assigned;

Just remember, System.DataRow.DataRowView will occur only if

ComboBox.SelectedValue is called before ValueMember assignment.




回答2:


setting the DisplayMember and ValueMemeber after setting the DataSource fixed this issue for me.

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



回答3:


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?

*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:

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.




回答4:


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




回答5:


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

    }



回答6:


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........

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

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




回答7:


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

for example:

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


来源:https://stackoverflow.com/questions/14620545/combobox-databinding-showing-system-data-datarowview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!