Andrew
Andrew

Reputation: 2839

Copying and inserting a row into a gridview based on button click

I have a GridView that gets its data from a SQL Server stored procedure. There are Item Templates in the grid to allow for data entry. I have a column with a CommandButton at the end of each row.

What I'm trying to accomplish: If the user clicks the button, a new row gets inserted which is a duplicate of the previous row. Scenario: while doing data entry, an item has more than one bin location. By duplicating the row, the end user can enter the 2nd bin location, where both records get dumped to a database.

I've been looking around and trying to filter out the remove duplicates from my research. Can this be done without putting the GridView into a datatable and rebinding? Is there a simpler way to do it server side? Does it need to be added in the footer?

Upvotes: 0

Views: 1007

Answers (1)

Yoinazek
Yoinazek

Reputation: 133

Unfortunately, you can't use the same row or same cells because the DataGridView only takes unique rows and cells. You'll have to get the values of that row and use them in a new row. I wrote a little bit of code to do this

string[] cells = (from c in datagridview.Rows[e.RowIndex].Cells.Cast<DataGridViewCell>()
                                    select c.Value.ToString()).ToArray();

DataGridViewRow row = new DataGridViewRow();
row.CreateCells(datagridview, cells);
datagridview.Rows.Add(row);

I hope I understood your question right. This is just something to get you started. I'm pretty sure it will not resolve your problem completely, but it's something.

Upvotes: 1

Related Questions