Solved

Adding Data from a datagrid

Posted on 2006-07-11
6
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: 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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

635 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