Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi folks,

user shall be able to edit a list of these cute little demo objects:
C#
class Foo
{
    public int FirstCol { get; set; }
    public int SecondCol { get; set; }
    public Foo(int first, int second)
    {
        FirstCol = first;
        SecondCol = second;
    }
}


public void WindowsFormsMethod()
{
    BindingList<Foo> list = new BindingList<Foo>();
    list.Add(new Foo(1, 2));
    list.Add(new Foo(2, 3));
    list.Add(new Foo(42, 32));

    dgvEditor.DataSource = bla;
    // dgvEditor.AllowUserToAddRows is already true
}
And by "edit", I mean he can change values, which works, _and_ add new rows, which doesn't. How can I enable user to add rows?
Posted
Updated 2-Dec-14 19:45pm
v2
Comments
Member 10889447 2-Dec-14 21:45pm    
Hope this link can help you.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.itemdatabound%28v=vs.110%29.aspx

As a result of googling a lot I've come to a decision that actually there is no way to add new row to a datagridview which has a binding source.However I am not sure at all.But According to your need I am giving you a solution I think it works for you fine.Please check it.Thanks


C#
public Form1()
  {
      InitializeComponent();
      BindingList<foo> list = new BindingList<foo>();
      list.Add(new Foo(1, 2));
      list.Add(new Foo(2, 3));
      list.Add(new Foo(42, 32));
      list.Add(new Foo(0, 0));
      dgvEditor.Columns.Add("FirstCol", "First Column");
      dgvEditor.Columns.Add("SecondCol", "Second Column");
      int count=0;
      foreach (Foo f in list)
      {
          dgvEditor.Rows.Add();
          dgvEditor[0, count].Value = f.FirstCol;
          dgvEditor[1, count].Value = f.FirstCol;
          count++;

      }
 }
 
Share this answer
 
v2
Comments
lukeer 15-Dec-14 2:03am    
While this solution surely would work for displaying items, editing and adding them in the DataGridView, it completely ignores the blessings of databinding and therefore leaves me with the burden of synchronizing the data and its view. In both directions.
Thank you for your effort, but I tend to use my own solution (see #2).
The solution is to add a parameterless Foo constructor.
C#
class Foo
{
    public int FirstCol { get; set; }
    public int SecondCol { get; set; }

    // This c'tor is needed for the user
    //   to add entries from within
    //   a databound DataGridView.
    public Foo()
    { }

    public Foo(int first, int second)
    {
        FirstCol = first;
        SecondCol = second;
    }
}


public void WindowsFormsMethod()
{
    BindingList<Foo> list = new BindingList<Foo>();
    list.Add(new Foo(1, 2));
    list.Add(new Foo(2, 3));
    list.Add(new Foo(42, 32));

    dgvEditor.DataSource = list;
    // dgvEditor.AllowUserToAddRows is already true
}

Which I tried to avoid because the parameterized c'tor did some checking of the values it got as parameters. Will have to perform those in another way.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900