adding and moving items in a bounded combo box RRS feed

  • Question



    I am using a binding source that is bound to a dataset with one table called AddressBook.

    The binding source is bound to a combo box.

    The address book table consists of 2 columns (number, name). There will be no duplicate phone numbers, as if it is already in the combo box it will
    just set it to index zero.

    This is just a simple table as I want to display the recent phone numbers.

    When someone enters a phone number it will add it to the combo box, and position the number as index 0 (most recent).

    However, if the phone number is already stored, then it should move it to index 0.

    So this just a simple combo box that displays the most recent calls that are made. Just like any mobile phone.

    This is my code so far. However, I am having a problem with moving the item to the top. is there some way this could be done better.

    Many thanks for any advice,


    Code Snippet
      //Item doesn't exist so add it and display as the first item
                if (!this.bsRedialedNumbers.Contains(this.txtDetails.Text))
                    DataRowView drv;
                    drv = (DataRowView)this.bsRedialedNumbers.AddNew();
                    drv["Name"] = this.txtDisplay.Text;
                    drv["Number"] = this.txtDisplay.Text;
                else //Item already exists so just move it to the top of the list.
                    //Move the item to the top of the combo box.
                    DataRowView drv;
                    drv = (DataRowView)this.bsRedialedNumbers;
                    int index = this.bsRedialedNumbers.Find("Number", txtDetails.Text);      



    Saturday, March 22, 2008 5:11 PM


  • My experience is too limited to have "definite" answers but here's a couple of thoughts.

    (1) Since you are deaing with DataRowViews, I'm guessing your datasource is a DataView (and thus not a datatable).  DataViews are good for sorting (sorting on the name column), but I doubt they are much good for "moving a particular row to the top.".   You could do this with a datatable (using a DataRow), but if you have to update the database, I would be a bit nervous about yanking a row out of the datatable, shoving it somewhere else in the datatable, and then trying to update the database - I would fear corrupting the database.


    Frankly I don' know of  a simple way to do what you are asking. I will make a suggestion, but even my suggestion will likely cause some inconveiniences for which you will have to write a lot of code to workaround them. My suggestion is to, in this case, abandon databinding. Instead do thie following. Stat with a dataview (in case you need to sort)


    DataView dv = datatable1.DefaultView

    dv.Sort = "Name" 'assuming you want to sort on the name.


    Now add each DataRowViwe to the combobox like this


    private class objRow


    public DataRow row;

    public override string ToString()


    return row["Name"] + " " + row["Number"];




    In other words loop through the dataview:

    foreach(DataRowView row in dv)


    objRow oRow = new objRow();

    oRow.row = row




    Anytime an object is added to

    a combobox, it calls the object's "ToString" function to determine how to display the object. That's why we override the ToString above, as to customize the display.


    To move an item to the top do something like this:


    objRow itemToMove = (objRow) combobxo1.SelectedItem;


    combobox1.Items.Insert(0, itemToMove);


    Saturday, March 22, 2008 11:54 PM