DataGridView - Insert Row

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

how do you insert a new row to a databound datagridview? It won't allow this
action.
 
Chris,

Can you show an example? What is it databound to? Is the
AllowUserToAddRows property set to true on the DataGridView? Or are you
getting an error when trying to do this programattically?
 
AllowUserToAddRows = true
I am trying to add them programatically.
I see an error that you cannot when the datagridview is bound to a dataset.
If the edit a row, then a new row appears automatically at the last row.

I need to "insert" a row before or after a selected row then add data to it.


Nicholas Paldino said:
Chris,

Can you show an example? What is it databound to? Is the
AllowUserToAddRows property set to true on the DataGridView? Or are you
getting an error when trying to do this programattically?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Chris said:
how do you insert a new row to a databound datagridview? It won't allow
this
action.
 
Try adding a row to the DataTable, and not the DataGridView

Try something like:

DataRow myNewRow;
myNewRow = myDataSet.myDataTable.NewRow(); // assuming typed dataset
myNewRow[0] = ...
myDataSet.myDataTable.Rows.Add(myNewRow);

Chris said:
AllowUserToAddRows = true
I am trying to add them programatically.
I see an error that you cannot when the datagridview is bound to a dataset.
If the edit a row, then a new row appears automatically at the last row.

I need to "insert" a row before or after a selected row then add data to it.


Nicholas Paldino said:
Chris,

Can you show an example? What is it databound to? Is the
AllowUserToAddRows property set to true on the DataGridView? Or are you
getting an error when trying to do this programattically?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Chris said:
how do you insert a new row to a databound datagridview? It won't allow
this
action.
 
Forgot too... when you add a row, it adds to the end.

Do you have a column that you can use for an index or sort on? I'd say if
there is, you may be able to add the row, set the key column, and then
refresh the datagridview? To borrow a very old method used in the old days
of BASIC...

you could have each row with a column having a sequence number, say 10, 20,
30, 40... and when you add a row, you could insert a row say at 15. You may
have to "re-align" the rows on the fly if you start adding a lot of rows
however.

I haven't tried it, but this almost seems like a task for a Doubly-Linked
list.

SerenityPCCS said:
Try adding a row to the DataTable, and not the DataGridView

Try something like:

DataRow myNewRow;
myNewRow = myDataSet.myDataTable.NewRow(); // assuming typed dataset
myNewRow[0] = ...
myDataSet.myDataTable.Rows.Add(myNewRow);

Chris said:
AllowUserToAddRows = true
I am trying to add them programatically.
I see an error that you cannot when the datagridview is bound to a dataset.
If the edit a row, then a new row appears automatically at the last row.

I need to "insert" a row before or after a selected row then add data to it.


Nicholas Paldino said:
Chris,

Can you show an example? What is it databound to? Is the
AllowUserToAddRows property set to true on the DataGridView? Or are you
getting an error when trying to do this programattically?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

how do you insert a new row to a databound datagridview? It won't allow
this
action.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top