There are plenty of resources to help you with Editing data in an ASPXGridview out there, but a majority of it is editing data that is DataBound to a Datasource (like a SQLDataSource for example)
This has been fine for me in the past, untill I started looking at
Retrieving WebConfig Values into DataTable project. Basically I needed to return the web.config values into a DataTable, and display them in a Grid, where they could be edited and the config file would be updated.
So in this instance I was unable to use the a SQLDatasource and had to manually bind the Data Table to the Grid.
That was the easy part.
The next bit was to get the data I had retrieved into the Grid to be Editable. And this is where I found the fountain of information began to ebb.
The Oasis of readily available information became dribbles of partial usefulnessess. I managed to figure it out in the end, but It's taken me a lot longer than usual to find this.
The actual process is pretty simple, but if you dont know it, you dont know it. So hopefully this will help any one in the same boat as I was.
Drag an ASPXGrid onto the page in Design view. Ensure that the
AutoGenerateColumns property is set to
true.
Switch to the Code view. To populate the grid programmatically use the following code:
ASPxGridView1.DataSource = dt
ASPxGridView1.KeyFieldName = "UniqueID"
ASPxGridView1.DataBind()
Note You do not need to add the KeyFieldName if youre just retrieving and binding the data, but you will need it for any of the events where you will be amending the data.
To create an
Edit button, use the following:
Protected Sub ASPxGridView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles ASPxGridView1.DataBound
If ASPxGridView1.Columns("Command") Is Nothing Then
Dim cmdCol As GridViewCommandColumn = New GridViewCommandColumn("Command")
cmdCol.EditButton.Visible = True
ASPxGridView1.Columns.Add(cmdCol)
End If
End Sub
If you have the KeyFieldName populated, when you compile & run the solution, when you click the edit button, it will put the grid into editing mode.
Finally, when youve updated the data, you will need to handle the event that is triggered when clicking the update button. This event is the
RowUpdating event.
Protected Sub ASPxGridView1_RowUpdating(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataUpdatingEventArgs) Handles ASPxGridView1.RowUpdating
Dim value1 As String = e.NewValues.Item(0)
Dim value2 As String = e.NewValues.Item(1)
CallUpdateDataBaseMethod(value1, value2)
ASPxGridView1.CancelEdit()
ASPxGridView1.DataBind()
End Sub
In this section you can define what happens in the code. In this example, I have taken the values of the first 2 columns and call a database editing method, which requires those values.
A point to note is as this event is primarily used to update databound datasources, we will need to cancel the editing process to get back to our normal data view.