views:

581

answers:

3

I was expecting this setting to affect the control itself, but I can't seem to add new rows just by having a DataGridView with AllowUserToAddRows set to true. Am I just rubbish at using my mouse and keyboard, or have I completely misunderstood this property? I suspect it's the latter, though I can't find much in the way of documentation to point me in the right direction.

EDIT: by the way, it's not that I'm rubbish, it seems to be something to do with using a List as the DataSource of the DataGridView; the little '*' just doesn't appear if I bind to a List.

+2  A: 

Try it,

DataTable dt = new DataTable();
dt.Columns.Add("No", typeof(int));
dt.Columns.Add("Name");
dataGridView1.AllowUserToAddRows = true;
dataGridView1.EditMode = DataGridViewEditMode.EditOnKeystroke;
dataGridView1.DataSource = dt;

EDIT:

Take a look at IBindingList

adatapost
I tried your code and it worked, so thanks :) But I'm actually using a List as the DataSource, which (since it's the only difference) is probably the cause of my problem. Do you know why a List would make the 'add row' functionality not work?
Ben Hymers
Implements **IBindingList** interface.
adatapost
+1  A: 

I solved this by changing STMTTRN from a List to a BindingList - List seems to have some very odd behaviour when used as a DataSource (see my other recent question, which is solved in the same way).

It's in generated code, but I'd already changed it from an Array to a List so BindingList is barely any extra trouble :)

Ben Hymers
A: 

I had the same problem.

using a BindingSource as the DGV datasource and setting the BindingSource property AllowNew = True, solved my problem.

Dim binding As New BindingSource
binding.DataSource = myList
binding.AllowNew = True
With DataGridView1
    .AutoGenerateColumns = False
    .DataSource = binding
End With
merlin