Add Record to Bound DataGridView

ssabc

Well-known member
Joined
Dec 11, 2017
Messages
63
Programming Experience
10+
Hello:

I have a bound DataGridView that I need to add data to. As a result, I cannot just add a row. I just want to add a row, and I suppose I have to figure out field 1 (the PK number), and 2 (the Job number from the Job Table. The table I'm speaking of is the JobDetail table, so everything is about that job.

I have seen a few examples, but they just don't work well.

In this case:
C#:
            DataTable dt = (DataTable)dgvJobDetail.DataSource;
            DataRow dr_Add = dt.NewRow();
            dr_Add[0] = "0000";
            dr_Add[1] = "1111";
            dt.Rows.Add(dr_Add);
            dt.AcceptChanges();
            dgvJobDetail.DataSource = dt;

I get a System.InvalidCastExxception error on the first line. 'Unable to cast object of type 'System.Windows.Forms.BindingSource' to type 'System.Data.DataTable'.'

I just want to add a new record with blank values, and specify the two dependent values for the data relationship.

Thanks,
 
You should not be calling AcceptChanges at all. It's rare that you ever should. As I said in another thread, you call Fill on a data adapter to retrieve data from a database into a DataTable and you need to call Update on that same data adapter to save changes from that DataTable back to the database.

Chnages in a DataTable are indicated by the RowState of each DataRow. When you call Fill on your data adapter, internally it will query the database and add a DataRow to the DataTable for each record, then call AcceptChanges to ensure that all RowStates are set to Unchanged. If you add a new DataRow to the DataTable, its RowState is set to Added. If you edit a DataRow in the DataTable, its RowState is set to Modified. If you delete a DataRow in the DataTable, its RowState is set to Deleted (it is NOT removed from the DataTable). When you call Update on your data adapter, it will execute SQL INSERT, UPDATE and DELETE statements against the database for each DataRow with a RowState of Added, Modified and Deleted respectively. Finally, AcceptChanges is called implicitly to set all RowStates back to Unchanged. By calling AcceptChanges yourself, you are setting all RowStates to Unchanged, indicating that there are no changes to be saved, without ever actually saving the changes.

Also, as I said in your other thread, you should bind your DataTable to a BindingSource and bind that to the grid. You can then create a new row by calling AddNew on the BindingSource, which will return a DataRowView via an Object reference, so you need to cast. You can set the fields of that DataRowView by index or column name, just as you are for the DataRow. You then add the row to the DataTable by calling EndEdit on the BindingSource.

Also, apart from anything else, there's no point assigning the DataTable to the DataSourc eproperty of the grid in the last line of your code above when you got that DataTable from the DataSource of the grid in the first place.
 
Back
Top Bottom