Solved

Adding Data from a datagrid

Posted on 2006-07-11
6
207 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

Industry Leaders: 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

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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 …

726 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