Solved

Adding Data from a datagrid

Posted on 2006-07-11
6
204 Views
Last Modified: 2011-09-20
Hello,

Is there a way to add data to a sql table from a datagrrid without writing every single column.

The situation is; I have a query which is a stored procedure. This outputs the results into a datagrid in vb 2005. when I edit a row in this datagrid i want to write a statement that updates the changes. Currently my code looks somthing like this:

UPDATE tablename
SET Name = Datagridview.Item(0,1).Value, Address = Datagridview.Item(0,2).Value, Postcode = ......etc  
WHERE Name = "Me"

I want some thing like

UPDATE tablename
SET rowdata
WHERE Name = "Me"

Thanks

0
Comment
Question by:Zanthras1st
  • 2
  • 2
6 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17086526
Does the DataGridView columns correspond with table column names in any way?

Bob
0
 

Author Comment

by:Zanthras1st
ID: 17088726
Yes they do as the table is generated by a query run on that table.
0
 

Author Comment

by:Zanthras1st
ID: 17088731
sorry the datagrid is generated by a query run oin that table
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 17090041
Here is what I was thinking:

1) If you are bound to the DataTable, and you have a DataAdapter, than making changes on the DataGridView should update the DataTable.  Then all you have to do is call the DataAdapter.Update method.

2) If you want to stick to the dynamic SQL route, then this is what I was alluding to before:

        Dim setClause As New System.Text.StringBuilder()

        For index As Integer = 0 To DataGrid1.Columns.Count - 1

            Dim column As DataGridViewColumn = DataGrid1.Columns(index)
            setClause.AppendFormat("{0} = '{1}'", column.Name, DataGrid1(index, 0).Value)

        Next index

        Dim commandText As String = String.Format( _
         "UPDATE tablename " & _
         "  SET Name = {0}" & _
         "  WHERE Name = '{1}'", setClause.ToString(), "Bob")

Bob
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
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 …

943 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