Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. DataGridView and new rows

DataGridView and new rows

Scheduled Pinned Locked Moved C#
question
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wjousts
    wrote on last edited by
    #1

    I have a collection (IList) of custom objects bound to a DataGridView. When you click on the * line to create a new row the DataGridView automatically creates a new object for you to edit. When it does this it calls the default constructor. What I would like to do is intercept this process so that I can either call a different constructor or else do some extra initialization before the DataGridView tries to bind to it. Is there a way to do this? The RowsAdded and UserAddedRow event both fire too late. Which method is the DataGridView calling to create a new row? I don't think it's actually getting added to the collection until the user edits the row. Thanks

    L 1 Reply Last reply
    0
    • W Wjousts

      I have a collection (IList) of custom objects bound to a DataGridView. When you click on the * line to create a new row the DataGridView automatically creates a new object for you to edit. When it does this it calls the default constructor. What I would like to do is intercept this process so that I can either call a different constructor or else do some extra initialization before the DataGridView tries to bind to it. Is there a way to do this? The RowsAdded and UserAddedRow event both fire too late. Which method is the DataGridView calling to create a new row? I don't think it's actually getting added to the collection until the user edits the row. Thanks

      L Offline
      L Offline
      Lisa Jorgensen
      wrote on last edited by
      #2

      One approach is to add a handler for the BindingSource's AddingNew event. E.g.:

      InitializeGrid()
      {
      // Create sample List of custom objects. First object uses a non-default
      // constructor, the second uses the default (no reason, just seemed
      // like something to do in the example).
      List<CustomItem> sourceList = new List<CustomItem>();
      CustomItem firstItem = new CustomItem("Special content");
      sourceList.Add(firstItem);
      CustomItem nextItem = new CustomItem();
      sourceList.Add(nextItem);

      // Create the BindingSource, add the list, and add an event handler
      // for the AddingNew event.
      BindingSource bindingSource = new BindingSource();
      bindingSource.DataSource = sourceList;
      bindingSource.AddingNew += new AddingNewEventHandler(bindingSource\_AddingNew);
      
      // Bind to the DataGridView.
      dataGridView.DataSource = bindingSource;
      

      }

      private void bindingSource_AddingNew(object sender, AddingNewEventArgs e)
      {
      // Use non-default constructor for the new object that will
      // be the source for the new row.
      e.NewObject = new CustomItem("Not default content");
      }

      An alternative is to use the DataGridView's DefaultValuesNeeded event, but the object has already been constructed by the time this event fires.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups