Programmatically add row to BindingSource ? RRS feed

  • Question

  • I'm trying to programmatically add a row to a BindingSource. This is what my code looks like:


    dsUserData.SearchFolderRow r = (dsUserData.SearchFolderRow)dsUserData.SearchFolder.NewRow();


    //columns in my table

    r.Path = dlg.SelectedPath;






    Basically, I'm displaying a FolderBrowserDialog, and I want to add the selected folder to the underlying data source. I'm getting this error: "Objects added to a BindingSource's list must all be of the same type."


    I don't know what to do. Any help appreciated.

    Thursday, August 16, 2007 4:35 AM


All replies

  • Calling the BindingSource's AddNew method will raise the AddingNew event where you can set the value for the new row



    Thursday, August 16, 2007 11:10 AM
  • Thanks for replying, but even after handling the AddingNew event, I get the same error.


    Here's what my code looks like:


    private dsUserData.SearchFolderRow _newSearchFolder;


    void NewSearchFolder_Click(object sender, EventArgs e)


    FolderBrowserDialog dlg = new FolderBrowserDialog();

    if (dlg.ShowDialog() == DialogResult.OK)


    _newSearchFolder = (dsUserData.SearchFolderRow)dsUserData.SearchFolder.NewRow();

    _newSearchFolder.Path = dlg.SelectedPath;



    //this line fails







    private void searchFolderBindingSource_AddingNew(object sender, AddingNewEventArgs e)


    e.NewObject = _newSearchFolder;


    Sunday, August 19, 2007 1:21 PM
  • Hi,


    Could this be of some help ? :





    Monday, August 20, 2007 2:32 AM