Solved

Adding Data from a datagrid

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

803 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