Frage

I am not setting my Gridview DataSource to a control like SqlDataSource or ObjectDataSource.
Instead I am Binding manually in the code behind using a method I wrote:

protected void Bind()
{
    CustomDepartmentGV.DataSource = GetSortedDepartments();
    CustomDepartmentGV.DataBind();
}

I am also manually handing update through the Gridview properties:

OnRowUpdating="UpdateRow"

And a simplified version of the UpdateRow method in the code behind:

protected void UpdateRow(object sender, GridViewUpdateEventArgs e)
{
    var oldValues = e.OldValues;
}

When I run the code above the oldValues variable is set to a System.Collections.Specialized.OrderedDictionary class but the keys and values collections within that have 0 keys / values.

After researching this quite a bit I found a couple posts that say that e.OldValues is not set unless you set the Gridview DataSource to a control like SqlDataSource or ObjectDataSource. I do not want to use these as I like the flexibility it gives me to write my own methods in code behind before hitting the data access layer objects.

Does anyone know how I can get e.OldValues working or provide an alternative so I can look at the original values of the row being updated in the UpdateRow method?

Note I am able to get the original values of the Gridview DataKey fields but I obviously don't want to set every column to be a data key.

War es hilfreich?

Lösung

Suppose the GetSortedDepartments() method returns a Datatable. You can put this datatable in the session (for example) and in the UpdateRow method use the Datakey of the the current row to get the old values from this datatable. Don't forget to update the datatable and to put it again in the session after a row is updated.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top