我有一个 GridView,我想在我的 ObjectDataSource 中使用自定义方法编辑/更新行。我可以使用自动生成的列而不是我需要的 BoundFields 来让它工作。我尝试在 RowUpdated、RowUpdating 和 RowEditing 事件中执行 GridView1.DataBind()。我没有收到错误消息。它只是不更新行。
事件更新.aspx
<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource2" AutoGenerateColumns="false">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="IncidentID" HeaderText="ID" ReadOnly="True" />
<asp:BoundField DataField="ProductCode" HeaderText="Product Code" ReadOnly="True" />
<asp:BoundField DataField="DateOpened" HeaderText="Date Opened" ReadOnly="True" />
<asp:BoundField DataField="DateClosed" HeaderText="Date Closed" />
<asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="True" />
<asp:BoundField DataField="Description" HeaderText="Description" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetCustomerIncidents" TypeName="IncidentDB" UpdateMethod="UpdateIncident">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="customerID" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="incidentID" Type="Int32" />
<asp:Parameter Name="dateClosed" Type="DateTime" />
<asp:Parameter Name="description" Type="String" />
</UpdateParameters>
</asp:ObjectDataSource>
App_Code\Service.cs
public static DataSet UpdateIncident(int incidentID, DateTime dateClosed,string description)
{
TechSupportDB techSupportDB = new TechSupportDB();
var myConnection = techSupportDB.GetConnectionString();
SqlCommand myCommand = new SqlCommand();
myCommand.Parameters.AddWithValue("@IncidentID", incidentID);
myCommand.Parameters.AddWithValue("@DateClosed", dateClosed);
myCommand.Parameters.AddWithValue("@Description", description);
myCommand.CommandText =
"update Incidents set DateClosed = @DateClosed, Description = @Description where IncidentID = @IncidentID";
myCommand.Connection = myConnection;
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
DataSet updatedIncidentsDataSet = new DataSet();
myAdapter.Fill(updatedIncidentsDataSet);
return updatedIncidentsDataSet;
}