Solved

Adding Data from a datagrid

Posted on 2006-07-11
6
203 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
Comment Utility
Does the DataGridView columns correspond with table column names in any way?

Bob
0
 

Author Comment

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

Author Comment

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

Accepted Solution

by:
Bob Learned earned 500 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

17 Experts available now in Live!

Get 1:1 Help Now