Please need help to populate a dropdownlist with ProductName and ProductID. Please see the image to see what are the values in the collection.
protected void Page_Load(object sender, EventArgs e)
{string gender = drpGender.DataTextField;
ProductCollection productos = ProductCollection.GetbyGender(gender);
IMAGE:
http://www.vengalia.com/OBJECTToPopulate.jpg
Loading

Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 9, 2009, 7:07 PM EST
Add this to your code.
string gender = drpGender.DataTextField;
ProductCollection productos = ProductCollection.GetbyGender(gender);
comboBox1.DataSource = null;
comboBox1.Items.Clear();
comboBox1.DataSource = productos;
comboBox1.DisplayMember = "ProductName";
comboBox1.ValueMember = "ProductID";
comboBox1.EndUpdate();
If this answer helps, please accept my answer. If not let me know.
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 9, 2009, 7:37 PM EST
Please mark my answer as accepted if it helped you. It will help me a lot.
You can even use it without beginupdate and endupdate methods.
But those methods are for performance increase.
This prevents the control from painting till endupdate is called.
You can refer to this for more info.
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.beginupdate.aspx
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 9, 2009, 7:22 PM EST
One line is missing int he previous reply. Use this.
string gender = drpGender.DataTextField;
ProductCollection productos = ProductCollection.GetbyGender(gender);
comboBox1.DataSource = null;
comboBox1.Items.Clear();
comboBox1.BeginUpdate();
comboBox1.DataSource = productos;
comboBox1.DisplayMember = "ProductName";
comboBox1.ValueMember = "ProductID";
comboBox1.EndUpdate();
Raul Juarez41 reputationPosted Nov 9, 2009, 7:28 PM EST
Thanks for your help it was great.
Please if you can give me your opinion about the following:
It works I just wonder about BeginUpdate() and EndUpdate() you used in your answer. Thanks for the answer.
ProductCollection productos = ProductCollection.GetbyGender(gender);
drpProductos.DataSource = null;
drpProductos.Items.Clear();
drpProductos.DataSource = productos;
drpProductos.DataTextField = "ProductName";
drpProductos.DataValueField = "ProductID";
drpProductos.DataBind();
Raul