Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

DataGridView C# Insert/Update/delete Events

Posted on 2009-04-07
3
Medium Priority
?
5,848 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 280 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 720 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

721 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