Solved

DataGridView C# Insert/Update/delete Events

Posted on 2009-04-07
3
5,725 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help on C# Linq query on object 6 62
show out valin of json for debugging in visaul c++ 1 31
Cant save 3D 4 19
Best environment for cross platform web development? 5 44
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

813 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

16 Experts available now in Live!

Get 1:1 Help Now