使用ObjectDataSource更新GridView BoundFields

时间:2014-04-22 01:39:39

标签: c# asp.net gridview objectdatasource boundfield

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

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;
}

3 个答案:

答案 0 :(得分: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']的原因

答案 1 :(得分:0)

嗨,我从你的问题中得到的一些东西,在第一次加载时,你得到你想要的东西(想要的结果)。然后你正在做一些事情(可能添加一些行,编辑或删除),你知道你正在哪一行进行操作。

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


    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;
    myCommand.ExecuteNonQuery();

这会将数据保存到数据库表中,并从基本查询中重新填充网格。通过这种方式,您将获得所需的结果。

答案 2 :(得分:0)

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

 <asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource2"
  AutoGenerateColumns="False" DataKeyNames="IncidentID">