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

If when creating a DataGrid, you set the
XML
<asp:commandfield showinsertbutton="true" />

you get an insert button next to each record. How does this work? When you click it, the code behind is called, but the values for the new record are simply the ones from the row in which the button was clicked, so I don't see how you can create a new record like this.

I must be missing something simple here.

An explanation would be very much appreciated (and may stop me going insane).


Kind wishes ~ Patrick
Posted

1 solution

See InsertCommand from the given link
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.commandfield.showinsertbutton(v=vs.110).aspx[^]

SQL
insertcommand="Insert Into [Customers]([CustomerID], [CompanyName], [City], [PostalCode], [Country]) Values (@CustomerID, @CompanyName, @City, @PostalCode, @Country)"
       connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
       runat="server">
 
Share this answer
 
v2
Comments
Patrick Skelton 28-Mar-14 13:28pm    
Thank you for that. The example shows the insert working with a details view, which - almost by definition - shows only a single record. That makes perfect sense. I was puzzled why something like the DataGrid can show an insert button on EVERY row. I can't see how that would ever be used.

- Patrick

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