Solved

DataGridView C# Insert/Update/delete Events

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

751 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