PDA

View Full Version : DataGrid Update Error


petersza
10-29-03, 04:16 PM
I have set up a DataGrid with and EditCommandColumn that defines an Update button, along with the usual Edit and Cancel buttons. When I click on the button to update a change made to data in the DataGrid after clicking the Edit button, I get this error:

System.InvalidOperationException: Update requires a valid UpdateCommand when passed DataRow collection with modified rows.

Here is the code for the datagrid:

<asp:DataGrid
Id="dgTrailers"
runat="server"
AutoGenerateColumns = "False"
CellPadding="5"
Width="100%"
OnEditCommand="dgTrailers_Edit"
OnCancelCommand="dgTrailers_Cancel"
OnUpdateCommand="dgTrailers_Update"
>
<HeaderStyle
BackColor="Black"
ForeColor="White"
Font-Bold="True"
HorizontalAlign="Center" />

<Columns>
<asp:BoundColumn
DataField="ID"
ReadOnly="True"
Visible="False"
/>
<asp:BoundColumn
DataField="Year"
HeaderText="Year"
/>
<asp:BoundColumn
DataField="Make"
HeaderText="Make"
/>
<asp:EditCommandColumn
ButtonType="PushButton"
UpdateText="Save"
CancelText="Cancel"
EditText="Edit"
/>
</Columns>
</asp:DataGrid>

Next is the code in the code-behind. daUtilityTrailer and dsUtilityTrailer are declared at the top of the page class, so they are in scope in the following proc:


Public Sub dgTrailers_Update(ByVal Sender As Object, ByVal e as DataGridCommandEventArgs)

Dim ID as Integer = e.Item.Cells(0).Text
Dim TrailerYear as Integer = CType(e.Item.Cells(1).Controls(0), TextBox).Text 'Use CType for types other than string.
Dim TrailerMake as String = e.Item.Cells(2).Text

OpenConnection("WebDataUtility")
OpenDataSet("Trailer")

Dim tbl As DataTable = dsUtilityTrailer.Tables("TrailerGrid")
tbl.PrimaryKey = New DataColumn() {tbl.Columns("ID")}
Dim row as DataRow = tbl.Rows.Find(ID)
row.Item("Year") = TrailerYear
row.Item("Make") = TrailerMake

daUtilityTrailer.Update(dsUtilityTrailer, "TrailerGrid")

CloseConnection("WebDataUtility")
CloseDataSet("Trailer")

dgTrailers.EditItemIndex = -1

BindGrid("tgTrailers")

End Sub


I don't get any complaints until I add in the line:

daUtilityTrailer.Update(dsUtilityTrailer, "TrailerGrid")


Thanks for the help!

Peter