Solved

DataGridView C# Insert/Update/delete Events

Posted on 2009-04-07
3
5,704 Views
Last Modified: 2013-12-14
Hi Experts.
Im trying to find out how to insert/update/delete rows in a databound datagridview in C#
I tried to search for some DEMO applications with source but came up short. My problem is that i am unable to find suitable events to pick up the data from the Inserted Row or updated row.
Now the delete part is easy where I am deleting the selected row, thus can pick up the data from the selected row property.

I have tried to use the rowLeave event and create a row from the e.RowIndex but then i only have value in the first Cell.
What events can be used to get the values from the cells. see code for example.
or if anyone knows of or has an demo application with source that i can use. don't Care if it is in VB, C , C++ , C# or java... all the same stuff :)

Regards

private void InsertRowToGroups(int RowIndex)

        {
 

            DataGridViewRow dr = odgMain.Rows[RowIndex];

           /*Trows Exception where Cell[1] is null. But Cell Count is 2*/

            MessageBox.Show(dr.Cells[0].Value.ToString() + " Is the value of cell 0\n " +dr.Cells[1].Value.ToString() + " Is the value of Cell 1 \n" );

        }

        

        private void odgMain_RowLeave(object sender, DataGridViewCellEventArgs e)

        {

            InsertRowToGroups(e.RowIndex);

        }

Open in new window

0
Comment
Question by:Sonnet
3 Comments
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 70 total points
ID: 24085881
Add, Edit, and Delete in DataGridView
Insert, Update, and delete single or multiple records in DataGridView.
http://www.codeguru.com/csharp/.net/net_data/datagrid/article.php/c13041
0
 
LVL 3

Accepted Solution

by:
WeiXi earned 180 total points
ID: 24085912
To which DataSource is the DataGridView bound? If it is bound to a DataTable, you don't have to care about inserting/updating/deleting the DataTable. That data binding takes care of that. After all, that's exactly what is there for.

If you want to trigger custom actions on inserting/deleting/editing a row in the DataGridView, the most straight-forward way is to use the following events of the DataTable:
DataTable.RowChanged
DataTable.RowChanging
DataTable.RowDeleted
DataTable.RowDeleting
DataTable.TableNewRow

Bound the DataSource of the DataGridView to a DataTable, then use above events of the DataTable and in the event handlers read the values directly from the DataTable.

I hope this is what you were looking for. If I have misunderstood your requirement, please clarify your needs.
0
 

Author Closing Comment

by:Sonnet
ID: 31567428
Thank you both Exelent responces. I used the Completely bound solution. instead of Creating the reader and the adapters by hand.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now