This is a migrated thread and some comments may be shown as answers.

Gridview Saving new data

8 Answers 289 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Matt
Top achievements
Rank 1
Matt asked on 23 Jan 2011, 01:05 AM
Hi all,

I've found literally thousands of examples, tutorials, videos and posts showing me how to use the gridview effectively to DISPLAY data..  I have that down pat... What I haven't been able to find are examples of how to SAVE new or updated rows back to the DB.

I know it works very easily using all the drag and drop within VS.. my issue is I have to work with SQL Server 2000, which is not supported with any VS drag and drop.. so I'm having to use a combination of business objects and data tables to bind to my gridview.

What I need is to know how to allow the user to change data in these grids and save the data back to SQL2000..

Any help would be greatly appreciated!

Thanks,

Matt

8 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 23 Jan 2011, 11:06 AM
Hello,

If you have a RadGridView bound to a DataTable, then as with other controls you need to call your DataAdapter.Update method. There is information here on Updating Data Sources with DataAdapters.

Hope that helps but let me know if you need further information
Richard
0
Matt
Top achievements
Rank 1
answered on 24 Jan 2011, 08:10 PM
Thanks Richard,

That was the solution.  What I ended up doing is restoring the DB to a SQL 2005 server, then building the Dataset in VS. Once it was all working I switched the connection back to teh SQL 2000 server.

Matt
0
Richard Slade
Top achievements
Rank 2
answered on 24 Jan 2011, 08:15 PM
Very glad you have it all working. A clever trick. All the best.
0
Matt
Top achievements
Rank 1
answered on 26 Jan 2011, 07:45 PM
Hi Richard,

Having another issue with updating the db...   Which event should I call tableadapter.Update(dataSet) from?

This is more a question of using SQLDataAdapter, but hopefully you might be able to point me in a direction...

I've tried CellValueChanged() and CellEndEdit(),  but what's happening is, If I change the value of a cell to say: 5555, the update runs, but doesn't actually do the update (the event does fire) ..  now if I edit the cell again and put in 7777, the update runs, but puts 5555, the original value into the db..

Watching SQL Profiler, the first time the event fires and runs tableadapter.Update(dataSet), nothing happens on the SQL Server side..
if I make a second change to the row, it then fires again, and this time runs agianst the SQL correctly, but putting in the first change to the row and not the second change.


Thanks!

Matt

One more thing I tried.. If I make a new Dataset and set it to myDataSet.GetChanges(), the first time CellValueChanged() fires, the new DS is null...  the second time it fires it then contains the 1st changes..  is there something I have to call in teh CellValueChanged() event to force it to first update the dataset with the grid changes before calling Update?
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 26 Jan 2011, 10:42 PM
Hello,

There are many different ways of updating the data, but here is one. Often though, I would find it best to save all the changes in one go rather than a cell by cell basis as a user can make mistakes and wants to change them. However, here is one way of updating your database with the changes.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.DataSet1.NameDescription.BeginLoadData()
    Me.NameDescriptionTableAdapter1.Fill(Me.DataSet1.NameDescription)
    Me.DataSet1.NameDescription.EndLoadData()
End Sub
Private Sub RadGridView1_RowsChanged(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.GridViewCollectionChangedEventArgs) Handles RadGridView1.RowsChanged
    If e.Action = Telerik.WinControls.Data.NotifyCollectionChangedAction.ItemChanged Then
        Dim rowData As DataSet1.NameDescriptionRow
        Dim rowView As DataRowView
        rowView = DirectCast(Me.RadGridView1.CurrentRow.DataBoundItem, DataRowView)
        rowData = DirectCast(rowView.Row, DataSet1.NameDescriptionRow)
        rowData.BeginEdit()
        rowData.Description = Me.RadGridView1.CurrentRow.Cells("Description").Value.ToString()
        rowData.Name = Me.RadGridView1.CurrentRow.Cells("Name").Value.ToString()
        rowData.EndEdit()
        Me.NameDescriptionTableAdapter1.Update(rowData)
    End If
End Sub

hope that helps
Richard
0
Matt
Top achievements
Rank 1
answered on 27 Jan 2011, 02:14 AM
Thanks again, Richard!

I will definitly keep this code stored away. After giving it second thought, you are corect, having it all update at once is a better idea.  So I've just given them a save changes button they can hit.

Thanks again!

Matt
0
James
Top achievements
Rank 1
answered on 01 Apr 2011, 05:21 AM
Hi Guys,

How can you save all in one go? (without using a button i mean) 
The row changed event seems to catch all cell changes. How can i get the row changed?

Like that instance where the pencil icon goes back to an arrow. When it updates the data source i need to know about it. (and push the change to a sql server)

any ideas?

Regards,
James
0
Julian Benkov
Telerik team
answered on 06 Apr 2011, 09:27 AM
Hello James,

You can use CurrentRowChanged event of RadGridView control in this scenario.

I hope this was helpful.

Regards,
Julian Benkov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Matt
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Matt
Top achievements
Rank 1
James
Top achievements
Rank 1
Julian Benkov
Telerik team
Share this question
or