1

我有一个 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;
}
4

3 回答 3

0

嗨,我从你的问题中得到的东西很少,在第一次加载时你得到了你想要的(期望的结果)。然后您正在做某事(可能添加一些行、编辑或删除)并且您知道您在哪一行上执行了哪些操作。

那么为什么不编写一个单独的函数来单独执行该活动。如果您要更新新行,请在 DB 中更新它并从数据库重新填充您的网格。

    TechSupportDB techSupportDB = new TechSupportDB();
    var myConnection = techSupportDB.GetConnectionString();    
    SqlCommand myCommand = new SqlCommand();    
    myCommand.Parameters.AddWithValue("@IncidentID", eventID);
    myCommand.Parameters.AddWithValue("@DateClosed", dateClosed);
    myCommand.Parameters.AddWithValue("@Description", description);
    myCommand.CommandText =
    “更新事件集 DateClosed = @DateClosed,描述 = @Description 其中 IncidentID = @IncidentID”;   
    myCommand.Connection = myConnection;
    myCommand.ExecuteNonQuery();

这会将数据保存到您的数据库表中并从您的基本查询中重新填充网格。这样你就会得到你想要的结果。

于 2014-04-25T03:16:09.703 回答
0

IncidentID 的 BoundField 设置为只读,因此用户无法编辑此字段,但这也阻止了它传递进行更新所需的值。解决方法:在 GridView 中设置 DataKeyNames = IncidentID。

 <asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource2"
  AutoGenerateColumns="False" DataKeyNames="IncidentID">
于 2014-04-28T01:14:20.987 回答
0

我认为你并没有真正用你的代码更新你的表。

在此处更改这部分代码:

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']

于 2014-04-22T03:31:13.730 回答