Solved

Update using DataGridView Control

Posted on 2006-12-01
6
223 Views
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)
        {
            try
            {
                for (int i = 0; i < ds.Tables.Count; i++)
                {
                    DataRow newRow = ds.Tables[0].NewRow();

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

                    ds.Tables[0].Rows.Add(newRow);
                }
                dataGridView.DataSource = ds.Tables[0];
                dataGridView.Refresh();
            }

0
Comment
Question by:LeeHenry
6 Comments
 
LVL 6

Expert Comment

by:nbkbar7
Comment Utility
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...

Dave

http://forums.mysql.com/read.php?38,115063,115063
0
 
LVL 29

Expert Comment

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

try
                  {
                        ((OleDbDataAdapter)FAdapter).SelectCommand
                              = new OleDbCommand
                              ("SELECT COMPANYCODE,USERNAME,ALERTCODE,OPERATOR,VALUE"
                              +" FROM TR_ALERTDET WHERE 1=2");
                        ((OleDbDataAdapter)FAdapter).FillSchema(FInternalDataSet
                              ,SchemaType.Mapped,"TR_ALERTDET");
                        ((OleDbDataAdapter)FAdapter).SelectCommand
                              = new OleDbCommand
                              ("SELECT COMPANYCODE,USERNAME,ALERTCODE,OPERATOR,VALUE"
                              +" FROM TR_ALERTDET WHERE USERNAME = @USERNAME");
                        ((OleDbDataAdapter)FAdapter).SelectCommand.Parameters.Add
                              ("@USERNAME",null);

                        ((OleDbDataAdapter)FAdapter).DeleteCommand
                              = new OleDbCommand
                              ("DELETE FROM TR_ALERTDET WHERE USERNAME = @USERNAME");
                        ((OleDbDataAdapter)FAdapter).DeleteCommand.Parameters.Add
                              ("@USERNAME",null);
                        ((OleDbDataAdapter)FAdapter).DeleteCommand.Parameters["@USERNAME"]
                              .SourceColumn = "USERNAME";
                        ((OleDbDataAdapter)FAdapter).DeleteCommand.
                              Parameters["@USERNAME"].SourceVersion
                              = DataRowVersion.Current;

                        ((OleDbDataAdapter)FAdapter).InsertCommand =
                              new OleDbCommand
                              ("INSERT INTO TR_ALERTDET(COMPANYCODE,USERNAME"+
                              ",ALERTCODE,OPERATOR,VALUE) VALUES(@COMPANYCODE,@USERNAME"+
                              ",@ALERTCODE,@OPERATOR,@VALUE)");
                        foreach(DataColumn Col in InternaDataTable.Columns)
                        {
                              ((OleDbDataAdapter)FAdapter).InsertCommand.Parameters.Add
                                    ("@"+Col.ColumnName,null);
                              ((OleDbDataAdapter)FAdapter).InsertCommand.Parameters["@"+Col.ColumnName]
                                    .SourceColumn =Col.ColumnName;
                              ((OleDbDataAdapter)FAdapter).InsertCommand.
                                    Parameters["@"+Col.ColumnName].SourceVersion
                                    = 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
((OleDbDataAdapter)FAdapter).Update(FInternalDataSet)
0
 
LVL 16

Expert Comment

by:AlexNek
Comment Utility
you must call the write command after wdit the data
in this sample
http://msdn2.microsoft.com/en-us/library/fbk67b6z.aspx
it is line
 dataAdapter.Update((DataTable)bindingSource1.DataSource);
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 6

Author Comment

by:LeeHenry
Comment Utility
Dave,

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

AlexNex,

I think your example was for SqlServer.

gauthampj,

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

thanks for the tips
0
 
LVL 6

Accepted Solution

by:
nbkbar7 earned 500 total points
Comment Utility

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...
http://www.uberasp.net/getarticle.aspx?id=37

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

Dave
0
 
LVL 6

Author Comment

by:LeeHenry
Comment Utility
kbkbar7,

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
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

10 Experts available now in Live!

Get 1:1 Help Now