我有一个GridView,我想在ObjectDataSource中使用自定义方法编辑/更新行。我可以使用自动生成的列来实现这一点,但我不需要BoundFields。我已尝试在RowUpdated、RowUpdating和RowEditing事件中执行GridView1.DataBind()。我没有收到错误消息。它只是不更新行。
IncidentUpdate.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>
应用代码\服务.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;
}
我认为您并没有真正用代码更新表。
在此处更改代码的这一部分:
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("SELECT * FROM Incidents ORDR BY IncidentId");
DataSet updatedIncidentsDataSet = new DataSet();
myAdapter.Fill(updatedIncidentsDataSet, "Incidents");
myAdapter.UpdateCommand = myCommand;
myAdapter.Update(updatedIncidentsDataSet, "Incidents");
return updatedIncidentsDataSet;
}
您需要指定要更新的tableName。在您的情况下,这是Incidents
尝试:
myAdpter.Update(updatedIncidentsDataSet, "Incidents");
如果我们不指定正在更新的表名,DataAdapter将使用默认的表名"Table"
,这就是为什么您将获得Update unable to find TableMapping['Table']
嗨,我从你的问题中得到了一些东西,即在第一次加载时,你得到了你想要的(期望的结果)。然后你正在做一些事情(可能添加一些行,编辑或删除),你就知道你在对哪一行执行哪个操作了。
那么,为什么不编写一个单独的函数来单独执行该活动呢。若您正在更新一个新行,那个么就在数据库中更新它,并从数据库中重新放置网格。
TechSupportDB-TechSupportDB=新的TechSupportDB();var myConnection=techSupportDB.GetConnectionString();SqlCommand myCommand=新的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;myCommand.ExecuteNonQuery()
这将把数据保存到DB表中,并从基本查询中填充网格。这样你就会得到你想要的结果。
IncidentID的BoundField设置为readOnly,因此用户无法编辑此字段,但这也阻止了它传递执行更新所需的值。解决方案:在GridView中设置DataKeyNames=IncidentID。
<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource2"
AutoGenerateColumns="False" DataKeyNames="IncidentID">