Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Best way to 'programmatically' add rows to a data-bound DGV?
 

Best way to 'programmatically' add rows to a data-bound DGV?

I have a standard dgv that is bound to a table in my dataset, and some combo & listbox controls that help narrow down a large selection of items that can potentially be placed into the dgv. My problem is how to get the selected item into the dgv and everything be happy. I tried this method:

int[] row = new int[] { currentRoomTemplateID, chosenPlumbingTypeID, 1 };
jPlumbingDataGridView.Rows.Add(row); //Call this method 1

but apparently rows cannot be programmatically added if the dgv is databound. So, I've been experimening with alternatives.

I can do a:
jPlumbingTableAdapter.Insert(currentRoomTemplateID, chosenPlumbingTypeID, 1); //Method 2

and it works, but I don't like the direction this takes because it bypasses the dgv and bindingsource, plus then I also have to refresh the dgv to show the new record, and dgv.Refresh() so far hasn't accomplished this.

I've also tried:
int[] row = new int[] { currentRoomTemplateID, chosenPlumbingTypeID, 1 }; //Method 3
this.sHC_DataSet.Tables["jPlumbing"].Rows.Add(row);

but this fails because the record I'm adding needs a PK and the datatable doesn't autogenerate one like the db does.

So, what's the best strategy to take? If there's absolutely no way to have the code add to the dgv (method 1), which is a better approach: method 2 or method 3?

Thanks in advance.

zeroPhase  Saturday, March 22, 2008 3:27 AM

Hi,

the prefered way is Method 3. It won't work in your case, but if you define your PK DataColumn as AutoIncrement=true and set AutoIncrementSeed and AutoIncrementStep the dataset is able to handle autogenerated columns.

Since you are using TableAdapters you must have a typed dataset. In this case is better to use

this.sHC_DataSet.jPlumbing.Rows.Add(row); // if the name of the DataTable is jPlumbing

instead

this.sHC_DataSet.Tables["jPlumbing"].Rows.Add(row);

because you get intelligence support and the named is checked at compile time.

There is still a better version to add a new row

jPlumbingRow newRow = this.sHC_DataSet.jPlumbing.NewjPlumbingRow();

newRow.RoomTemplateID = 12;

newRow.AnotherColumn = "dgdgdg";

this.sHC_DataSet.jPlumbing.AddjPlumbingRow(newRow);

regards

Philipp

Philipp Merz  Saturday, March 22, 2008 12:12 PM

Hi,

the prefered way is Method 3. It won't work in your case, but if you define your PK DataColumn as AutoIncrement=true and set AutoIncrementSeed and AutoIncrementStep the dataset is able to handle autogenerated columns.

Since you are using TableAdapters you must have a typed dataset. In this case is better to use

this.sHC_DataSet.jPlumbing.Rows.Add(row); // if the name of the DataTable is jPlumbing

instead

this.sHC_DataSet.Tables["jPlumbing"].Rows.Add(row);

because you get intelligence support and the named is checked at compile time.

There is still a better version to add a new row

jPlumbingRow newRow = this.sHC_DataSet.jPlumbing.NewjPlumbingRow();

newRow.RoomTemplateID = 12;

newRow.AnotherColumn = "dgdgdg";

this.sHC_DataSet.jPlumbing.AddjPlumbingRow(newRow);

regards

Philipp

Philipp Merz  Saturday, March 22, 2008 12:12 PM
Thanks for the answer Philipp, I wasn't aware of a tablenameRow object off of a datatable but this is for sure the slickest way.
zeroPhase  Saturday, March 22, 2008 2:39 PM

You can use google to search for other answers

Custom Search

More Threads

• how to stop my usercontrol connect to database in design-time
• DataGridView update problem
• DataGridView Does Not Show Vertical ScrollBar
• Check if drag drop item is image
• DataBinding vs Manual Iteration
• No Server Explorer or Data Sources
• what is the proper way to update joined tables without concurrency violation
• How print all record/selected record in data grid?
• datagridview row painting
• DATABINDING AND BINDING CONTEXT