Link to home
Start Free TrialLog in
Avatar of lyptus
lyptus

asked on

Add Items to a combo-box

Hi,
   I have a combo-box which gets populated with values from the database by specifying the data source property. However is it possible to add another item to the combo-box( say something like All)  before or after getting the items  from the database.

Thanks,
Aditya
Avatar of Mohammed Nasman
Mohammed Nasman
Flag of Palestine, State of image

comboBox1.Items.Insert(0,"All");
Avatar of sprisoner
sprisoner

Yes, I think finally I got it. Have been trying this for couple of hours. Ok...here is the solution. I hope, u are calling a dataview through the datasource property to pouplate the combo. So, it must be something like this:

<asp:DropDownList Runat="server" Id="ddlType" DataSource='<%# Type_GetTypes() %>' DataTextField="fldType" DataValueField="fldId" />

and in C#, u r defining a method:

public DataView Type_GetTypes()
{
     return(datawrapper.ExecuteQuery("select fldId, fldType from tblTypes"));      
}

So, this method populates ur combo. What happens here is the methos returns a dataview which populates ur combo.
Now, all u have to do is add a new row in the dataview. So the method will be:

public DataView Type_GetTypes()
{
      DataView dvi = datawrapper.ExecuteQuery("select fldId, fldType from tblTypes");      
      DataRowView drv = dvi.AddNew();
      drv["fldId"] = "99";
      drv["fldType"] = "All";
      return(dvi);
}

U must have understood it. I m getting the dataview as usual and then adding a row as per my requirement. This will be added to ur combo as well.

Regards,
Nash
Avatar of lyptus

ASKER

HI,
   The application I am writing is a Windows app. Shown below is what i am doing
this.cboDept.DataSource = emp.DTDept;
this.cboDept.DisplayMember = "name";
this.cboDept.ValueMember = "deptID";

If i try the add or the insert after this piece of code it blows up saying I cannot modify the items in the combo-box once the datasource is set.
If i try the add or the insert before setting the datasource the All does not show up in the combo-box.

Thanks,
Aditya
 
 
ASKER CERTIFIED SOLUTION
Avatar of Mohammed Nasman
Mohammed Nasman
Flag of Palestine, State of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial