DataGridView new row problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LukasMalik
    New Member
    • Jan 2008
    • 22

    DataGridView new row problem

    Hi,

    I have .NET 3.5 Windows Forms application which contains several DataGridView controls. Application also contains class Item (represents price list item) and ItemCollection with implemented IEnumerable, IList interfaces.

    dataGridView1 has set AllowUserToAddR ows property = true;

    When I set DataSource:
    [code=cpp]
    dataGridView1.D ataSource = oInvoice.Items; //returns ItemCollection. .[/code]

    there is no blank row for new record in dataGridView1.

    When I set DataSource:
    [code=cpp]
    dataGridView1.D ataSource = oInvoice.GetIte ms(); //returns DataTable..[/code]

    works everything fine, there is new blank row at end of dataGridView1 rows.

    Any advices?

    Thank a lot
    Last edited by Frinavale; Dec 23 '08, 02:47 PM. Reason: Added [code] tags and Moved Thread to C# forum.
  • nukefusion
    Recognized Expert New Member
    • Mar 2008
    • 221

    #2
    According to MSDN, if the grid is bound to data, the user is only allowed to add rows if both the AllowUserToAddR ows property and the datasource's IBindingList.Al lowNew property are set to true.

    When you set up a DataTable as the datasource, the grid is binding to the DataTable via DefaultView. This means it works because although DataTable itself doesn't implement IBindingList, the DataView object does.

    ItemCollection doesn't implement IBindingList, so presumably you won't be able to add rows.

    In order to fix it you would probably use a different collection type that does implement IBindingList or create a custom collection type that implemented IBindingList yourself (a lot more work)

    Comment

    • phvfl
      Recognized Expert New Member
      • Aug 2007
      • 173

      #3
      If using databinding the easiest built-in object I have found to use is System.Componen tModel.BindingL ist<T>. This implements the iBindingList interface and can be set up as easily as the ItemCollection.

      It does have some limitations with sorting, however, casting can expose this methods.

      Comment

      Working...