Maslow
Maslow

Reputation: 18746

How do I Add a new row to a datagridview without using a dataset or datatable?

I'm using a List to bind to a dataGridView. I'd like the user to be able to add a new row.

I've tried using a BindingList and a BindingSource.

Here's my binding (not including the combo box column I add to the datagridview before adding a datasource:

binding = new BindingSource();
binding.DataSource= _Rows;
_dg.DataSource = binding; 

Here's the code that fills _Rows

protected override List<BusinessApp> getRows(DataGridViewManager manager)
    {
        var dc = manager.dc.DcHerb;
        return (from p in dc.apps
                where p.zQueueId == this.ZQueueId
                select p)
                      .ToList().ToBusinessApp(dc);
    }

Here's what I'm trying to get a new row. No exception occurs, but the dataGridView doesn't appear to change at all, even though _Rows count goes from 68 to 69

private void newRow()
    {

        _Rows.Add(new BusinessApp(_manager.dc.DcHerb, new app() {acaps="1234"  }));

        //_dg.DataSource = binding; //tried rebinding here
        //_dg.DataSource = _Rows;
    }

Upvotes: 0

Views: 1987

Answers (2)

Arun
Arun

Reputation: 2523

How about invoking "AddNew()" method on your "binding" member variable that you have defined already?

binding.AddNew();
// and set the needed columns...
((binding.Current.Row) as <yourTypedDataTableRow>).<columnName> = <someValue>;

Upvotes: 0

Joshua Lee
Joshua Lee

Reputation:

I would as if you have any foreign key bind to the other table or not, if yes, you may assign a key to the new added row and the datagrid will show the current new added row. Otherwise you may also missing the code datagrid.EndEdit()

Upvotes: 0

Related Questions