Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DataGridView C# Insert/Update/delete Events

Posted on 2009-04-07
3
Medium Priority
?
5,885 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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

876 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