Add Row to DataGrid with Controlled Content

Jul 19, 2009 at 6:50 PM

I am attempting to write code that controls the content of a row added to a SourceGrid.DataGrid.  The grid is populated by a table in a sql server database, I use the grid to edit the data and write back to the database.  I want each added row to be a copy of the first row of the table - the new rows can then be edited by the user, before saving the grid back to the table.  My problem is that the grid updates and displays the newly added rows when I add rows as shown below, they are editable as I desire, but the new rows are not saved back to the database.  If I set AllowNew for the grid (bd.AllowNew=true), then a double click in the bottom row creates a blank row, which does get saved back to the database when I execute the save.  If I do not set AllowNew, then the only way to add a row is through the AddNew button, which gives me the kind of control I want over the content of the added row.  The added rows appear in the grid, can be saved to a csv file (through code from frmSample1.cs), but DO NOT get saved back to the database.  I have tried a bunch of things, nothing seems to work, any help much appreciated.

My add row code is as follows:

private void btnAddNew_Click(object sender, EventArgs e)
            DataRow drow = dataset.Tables[0].Rows[0];
            drow[0] = System.DBNull.Value;
            bd.AllowNew = true;
            // add a copy of first row, just to allow editing

the save back to the database is handled by:

private void btnSave_Click(object sender, EventArgs e)


and the binding to the grid is as follows:

dataset = new System.Data.DataSet();
adapter = new System.Data.SqlClient.SqlDataAdapter(txtSQLQuery.Text, sConnectionString);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(adapter);
 bd = new DevAge.ComponentModel.BoundDataView(dataset.Tables[0].DefaultView);
 dataGrid1.DataSource = bd;



Thanks in advance, if the above is unclear I will be happy to try to clarify.

Dic,k Males

Cincinnati, OH, USA