|
-
Mar 14th, 2007, 10:59 AM
#1
Thread Starter
PowerPoster
DGV Updates every row with the same data????
I have a DataGridView that updates each row with the data of the last row. What cause this?
Thanks,
-
Mar 14th, 2007, 05:40 PM
#2
Re: DGV Updates every row with the same data????
 Originally Posted by blakemckenna
I have a DataGridView that updates each row with the data of the last row. What cause this?
Thanks,
Your code causes it. As you haven't provided the code there's no way to be more specific.
-
Mar 14th, 2007, 05:54 PM
#3
Thread Starter
PowerPoster
Re: DGV Updates every row with the same data????
Sorry JMC, here it is as an attachment!
-
Mar 14th, 2007, 06:30 PM
#4
Re: DGV Updates every row with the same data????
This is how you save the changes from a DataTable to a database. Note the use of parameters and note that there are no literal values specified. The parameters specify which column of the DataTable the data is to be drawn from. Then when you call Update it can save every row by using the values contained in that row:
vb Code:
Dim con As New SqlConnection("connection string here")
Dim adp As New SqlDataAdapter("SELECT ID, FirstName, LastName FROM Customer", con)
Dim del As New SqlCommand("DELETE FROM Customer WHERE ID = @ID", con)
Dim ins As New SqlCommand("INSERT INTO Customer (FirstName, LastName) VALUES (@FirstName, @LastName)", con)
Dim upd As New SqlCommand("UPDATE Customer SET FirstName = @FirstName AND LastName = @LastName WHERE ID = @ID", con)
del.Parameters.Add("@ID", SqlDbType.Int, 4, "ID")
ins.Parameters.Add("@FirstName", SqlDbType.VarChar, 50, "FirstName")
ins.Parameters.Add("@LastName", SqlDbType.VarChar, 50, "LastName")
upd.Parameters.Add("@FirstName", SqlDbType.VarChar, 50, "FirstName")
upd.Parameters.Add("@LastName", SqlDbType.VarChar, 50, "LastName")
upd.Parameters.Add("@ID", SqlDbType.Int, 4, "ID")
adp.DeleteCommand = del
adp.InsertCommand = ins
adp.UpdateCommand = upd
Dim tbl As New DataTable
adp.Fill(tbl)
'Make changes to data here.
adp.Update(tbl)
Note that everything before the declaration of the DataTable can, and in my opinion should, be done in the designer.
Note also that the grid is nowhere to be seen. Binding to the grid and making changes through the UI is all incorporated into that one comment and is irrelevant to the process of retrieving and saving the data.
-
Mar 15th, 2007, 09:23 AM
#5
Thread Starter
PowerPoster
Re: DGV Updates every row with the same data????
By using Parameters, I'm assuming your using stored procedures...correct? And by the way JMC...that's a great example!
-
Mar 15th, 2007, 11:30 AM
#6
Thread Starter
PowerPoster
Re: DGV Updates every row with the same data????
I tried using your code and it didn't update a row...so I'm thinking the parameters are for SP's only.
-
Mar 15th, 2007, 04:19 PM
#7
Re: DGV Updates every row with the same data????
There is no reference to stored procedures in my code. It's got four literal SQL statements in it. Anyway, it doesn't matter whether you use stored procedures or not, parameters are parameters. If I created stored procedures containing those SQL statements my parameters would still be EXACTLY the same. My code is an example only. If you have a table named "Customer" that has ID, FirstName and LastName columns of type int (identity), varchar(50) and varchar(50) respectively then it will work exactly as it's supposed to. You can delete, add and edit rows where I've put the comment and all the changes will be saved afterwards.
If you want to test that code specifically then you're going to have to create a table with that schema. If you want to test it in your actual situation then you need to adapt the principles to that situation. The principles are:
1. Create a DataAdapter with a SelectCommand.
2. Create a DeleteCommand, InsertCommand and UpdateCommand as needed. These can use literal SQL statements or stored procedures. IT DOES NOT MATTER WHICH.
3. Add parameters to your commands. Do not set the Value of ANY parameter. Add them with a name, type, size and source column.
4. Add the commands to the adapter.
5. Populate a DataTable by calling the adapter's Fill method.
6. Make changes to the data in the DataTable as required. It doesn't make ANY difference whatsoever how this achieved.
7. Save the changes in the DataTable by calling the adapter's Update method.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|