I'm creating a program to track our company's IT assets. Each item gets its own row so we can individually track where it is and who has it. I want to give the user the option to copy and duplicate rows, so they don't have to type in the exact same thing for 50 power cords. When I try to use datagridview.rows.addcopy, I get an error message saying that rows cannot be programatically added to a control that is databound. Are there any ways around this?
You may set the DataGridView.DataSource to a DataTable. Then, when a user selects a row in the datagridview, the content should be first added to the datatable as a new row and again set the datagridview.datasource to this datatable, the change will be reflected. Other than that you should write code to update the underlying table in the database.
It is assumed the datagridview (dgv) has its DataSource set to a DataTable dt.
code: you can invoke this method as: copyRow(dgv.CurrentRow);
private void copyRow(Int32 irow)
{
DataRow dr = dt.Rows[irow];
//if you have a unique identifier for this row, then
//before adding as a new row , generate a new id
//replace the current id in this row with this new value
//assuming 0 element of dr has the unique id, set dr[0] = <new id>;
dt.Rows.Add(dr);
//write code to submit the changes done to DataTable, back to database
dgv.DataSource = dt;
}