Finding values in a databound combobox

I have a set of two comboboxes. When the value of combo1 changes I need to change the selecteditem of combo2. The selecteditem of combo2 is related to the particular value selected in combo1.

The mechanics of this is easy enough but I'm having a problem.

Since the combobox (combo2) is databound, its items are DataRowViews. This means I cannot use items.indexof to search for my value. combo2.FindString and combo2.FindStringExact will not work in this instance because I need to search by the value.

I can easily find the DataRow in the original DataSource.DataTable that the value is in but how do I convert it to a DataRowView which I can search on using indexof

Or can someone think of a better way

Thanks,
Scott


Answer this question

Finding values in a databound combobox

  • gman108362

    Can you use the Find method on your DataSource to get the index of the item The combobox and the datasource are in the same listing, so finding the item in the datasource will be the same index in the combobox.< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     

    For example, the BindingSource component has a Find method that will return the index.

     

    -mark

    Program Manager
    Microsoft
    This post is provided "as-is"

     


  • Shamus Xu

    The DataRowView comes from the view that the DataTable creates (DefaultView). Each row in the view is a DataRowView. < xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     

    But that is about as far as I understand < xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />ADO and DataTables/Views. I suggestion asking more questions on the .NET Framework Data Access and Storage where the ADO team hangs out at: http://forums.microsoft.com/MSDN/ShowForum.aspx ForumID=45&SiteID=1

     

    -mark

    DataGridView Program Manager
    Microsoft
    This post is provided "as-is"

     

     


  • Orlanzo

    I will do that.

    Thanks again for your time.

    Scott

  • RomeoChua

    Thank you. That solution works great.

    Just to satisfy my curiosity, where do the datarowviews in the binding source come from More specifically is there a way to convert a row from the source take to its datarowview equivelant in the bindingsource.

    To illustrate.

    DataSource ds;
    BindingSource.DataSource = ds;
    BindingSource.DataMember  = "MyTable"; //Name of table inside of ds
    ComboBox.DataSource = BindingSource;
    ComboBox.DataMember = "MyColumnName";
    ComboBox.ValueMember = "MyValueColumnName";

    at this point...
    BindingSource.List ~= ComboBox.Items
    (i.e. they contain the same datarowviews in the same order)


    Now lets say I have:
    DataRow r = ds.Tables["MyTable"].Rows[5]; //Some random row

    assuming no sorts or filters on the bindingsource or combobox...
    r contains the same essential data as BindingSource[5] == BindindSource.List[5] == ComboBox.Items[5]

    But just for the sake of argument, lets say the binding source is filtered and sorted, thus removing the orgininal "in-order" index matching between ds.Tables["MyTable"].Rows and ComboBox.Items (i.e. ds.Tables["MyTable"].Rows[5] != ComboBox.Items[5])

    Now, with only knowing r, is it possible to convert, perhaps using some method or property of bindingsource, to convert r into a DataRowView which can be used with BindingSource.List.IndexOf

    Thanks,
    Scott


  • Finding values in a databound combobox