Update using DataGridView Control

Posted on 2006-12-01
Last Modified: 2010-04-16
I am trying to teach myself how to use the DataGridViewControl.  What I would like to do is simply display data from a table of my MySql Database, and then click on any row item, edit the item,  and update my mysql databse after the user presses enter or clicks save.

 I have been able to display data from one of my MySql tables, but I can't seem to get a grasp on how I can  update my database with the edited cell. It appears as this would be easier if I was using SqlServer, but i must use MySql. I posted my code that I used to load the DataGridViewControl.

private void InitializeDataGridView(DataSet ds)
                for (int i = 0; i < ds.Tables.Count; i++)
                    DataRow newRow = ds.Tables[0].NewRow();

                    newRow.ItemArray = ds.Tables[i].Rows[0].ItemArray;

                dataGridView.DataSource = ds.Tables[0];

Question by:LeeHenry

Expert Comment

ID: 18053093
This what you're looking for?

If not lem'me know and I'll throw together some code and we can step through it together...

LVL 29

Expert Comment

by:Gautham Janardhan
ID: 18053210
IDataAdapter FAdapter = new OleDbDataAdapter(UrConnection);
DataSet FInternalDataSet = new DataSet(); // replace with ur dataset

                              = new OleDbCommand
                              +" FROM TR_ALERTDET WHERE 1=2");
                              = new OleDbCommand
                              +" FROM TR_ALERTDET WHERE USERNAME = @USERNAME");

                              = new OleDbCommand
                              ("DELETE FROM TR_ALERTDET WHERE USERNAME = @USERNAME");
                              .SourceColumn = "USERNAME";
                              = DataRowVersion.Current;

                        ((OleDbDataAdapter)FAdapter).InsertCommand =
                              new OleDbCommand
                              ("INSERT INTO TR_ALERTDET(COMPANYCODE,USERNAME"+
                        foreach(DataColumn Col in InternaDataTable.Columns)
                                    .SourceColumn =Col.ColumnName;
                                    = DataRowVersion.Current;
                  catch(Exception ex)
                        throw new Exception("Exception in prepare commands "+ex.Message);

i'm ising a table called tr_alertdet and where username is the primary key  u can change the command for ur table respectively

while saving just call
LVL 16

Expert Comment

ID: 18053564
you must call the write command after wdit the data
in this sample
it is line
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.


Author Comment

ID: 18058785

I saw this example on the mysql forums. I've already tried to get this to work.  When I copy and paste the exact code, and use the country database like they mention, I can display the information, but nothing else is working. I set breakpoints on all the other functions (dataGridView_RowValidating etc...), and they are not reaching them. I didn't see anything in the properties of the dataGridView where I needed to specify these functions anywhere. I am probably missing something simple..??? any suggestions


I think your example was for SqlServer.


I'm going to take a look at yours later on tomm..

thanks for the tips

Accepted Solution

nbkbar7 earned 500 total points
ID: 18141375

Holy Cow! LeeHenry I am SOOO sorry, I totally dropped the ball on your question!! Somehow I let it slip through the cracks and I missed your reply, I apologize!!

To answer your question (if you don't already have an answer) it sounds like you're almost there and you just need to wire up the events that you need to use. This is actually done in Initialize components (or you can do it anywhere you need to). basically it follow the standard delegate format that you may or may not have seen in windown forms.

dataGridView.RowValidating += new System.EventHandler(this.NameOfYourFunction);

The whole process can be confusing because Microsoft isn't very consistant in the implementation. It called wiring up an event.

This link gives a really good explaination of event wiring and how to do it when the IDE doesn't do it for you...

Again I apologize for dropping the ball here... Lem'me know how it goes and if yo uhave anymore questions...


Author Comment

ID: 18160970

I'm not sure if i'm going to work on this again.. i've been busy with something else... If i find out i'm not going to get into this, i'll go ahead and  give you the points since you were very helpfull..  I should be letting you know soon.. thanks again for the response

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 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