locked
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;

    r.SetLastIndexedNull();

     

    searchFolderBindingSource.Add(r);

     

     

    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

Answers

All replies

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

     

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.bindingsource.addnew.aspx

    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;

    _newSearchFolder.SetLastIndexedNull();

     

    //this line fails

    searchFolderBindingSource.AddNew();

     

    UserData.BuildFolderIndex(dlg.SelectedPath);

    }

    }

     

    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 ? :

     

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=18810&SiteID=1

     

    Luc

    Monday, August 20, 2007 2:32 AM