Click here to Skip to main content
15,033,147 members
Please Sign up or sign in to vote.
3.40/5 (2 votes)
See more:
On a Win Form I have two combo boxes; one combobox is dependent upon another.
i have these code
C#
private void Form2_Load(object sender, EventArgs e)
        {
DataTable dataTable = objProdCat.SelectAll();
ddlCategory.DataSource = dataTable;
ddlCategory.DisplayMember = "ProductCatName";
ddlCategory.ValueMember = "ProductCatId";
        }
//another cb code below
private void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (ddlCategory.SelectedValue != null)
                {
                    objProdSubCat.ProductCatId = Convert.ToInt32(ddlCategory.SelectedValue);
                    BindSubCat();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

but the problem is it showing me this exception..
how to solve it??
Unable to cast object of type 'system.Data.DataRowView' to type 'System.IConvertible'
Posted

first mention valuemeber and displaymember the bind datasoure as below code

private void Form2_Load(object sender, EventArgs e)
        {
ddlCategory.DisplayMember = "ProductCatName";
ddlCategory.ValueMember = "ProductCatId";
DataTable dataTable = objProdCat.SelectAll();
ddlCategory.DataSource = dataTable;
        }
   
Comments
Muhamad Faizan Khan 26-Apr-14 6:23am
   
when i am trying above code it show me only numbers why
Muhamad Faizan Khan 26-Apr-14 6:29am
   
i debug above code. after ddlCategory.DisplayMember = "ProductCatName";
ddlCategory.ValueMember = "ProductCatId"; it going to objFrmProduct.Show(); not going to ddlCategory.DataSource = dataTable;why. i guess that why it is not working
[no name] 26-Apr-14 6:32am
   
see this method "objProdCat.SelectAll();" where it is defined
[no name] 26-Apr-14 6:32am
   
and that's returning list of values or not
Muhamad Faizan Khan 26-Apr-14 7:37am
   
+5 fine but i am stuck with another error
[no name] 26-Apr-14 14:13pm
   
thanks
Volynsky Alex 26-Apr-14 10:19am
   
Nice answer
[no name] 26-Apr-14 14:11pm
   
thanks
Volynsky Alex 26-Apr-14 17:12pm
   
You're welcome
Error is on below line
objProdSubCat.ProductCatId = Convert.ToInt32(ddlCategory.SelectedValue);

change it to
objProdSubCat.ProductCatId = Convert.ToInt32(((DataRowView)ddlCategory.SelectedValue)["ProductCatId"]);

refer Unable to cast object of type 'System.Data.DataRowView' to type 'System.IConvertible'[^]
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900