0

我正在处理一些我以前没有处理过的新概念,所以我可能过于复杂了。

我有一个从 5 表连接填充的 GridView。每列代表联接中的 5 个表之一。我想要实现的功能是这样的:

  1. 用户选择自动生成的编辑链接按钮
  2. 用户编辑行中的一个或多个单元格
  3. 用户选择自动生成的更新链接按钮
  4. 参数被传递给更新受影响的表的存储过程。

我对如何进行有一些不确定性。如果我直接查询更新,我需要从另一列传递 ID、更改的文本和外键 ID。所以我的问题是如何让 GridView UpdateCommand 将整行的数据传递给存储的 Proc?我目前已经用一个额外的参数设置了存储过程,我用它来确定需要更新哪个表,但由于它可能是多个表,我认为这是错误的方法。这是我的 GV、DSN 和 SP。

 <asp:GridView ID="GridView1"
    runat="server"
    AutoGenerateColumns="False" 
    DataKeyNames="id" DataSourceID="SqlDataSource1" 
    EnableModelValidation="True"
    AllowSorting="True" 
    EnableSortingAndPagingCallbacks="True"
    allowpaging="True"
    pagesize="26"
    ondatabound="GridView1_DataBound">    

        <Columns>
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="True"
 ReadOnly="True" SortExpression="id" />
            <asp:BoundField DataField="Manufacturer" HeaderText="Manufacturer"   
SortExpression="Manufacturer" />
            <asp:BoundField DataField="PriModel" HeaderText="PriModel" 
SortExpression="PriModel" />
            <asp:BoundField DataField="SecModel" HeaderText="SecModel"  
SortExpression="SecModel" />
            <asp:BoundField DataField="Form" HeaderText="FormFactor" 
SortExpression="Form" />
            <asp:BoundField DataField="Active" HeaderText="Active" 
SortExpression="Active"/>
            <asp:CommandField ShowEditButton="True" />                
        </Columns>
       </asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" DataSourceMode="DataSet"
    ConnectionString="<%$ ConnectionStrings:inventory_v2ConnectionString %>" 
    SelectCommand="SELECT dbo.Models.ID,dbo.Models.Active, 
    dbo.Models_PriModels.PriModel, dbo.Models_SecModels.SecModel, 
    dbo.Models_OEMs.Manufacturer, dbo.Models_FormFactor.Form
    FROM dbo.Models INNER JOIN dbo.Models_FormFactor ON 
    dbo.Models.FormFactor = dbo.Models_FormFactor.ID INNER JOIN
    dbo.Models_OEMs ON dbo.Models.Manufacturer = dbo.Models_OEMs.ID INNER JOIN
    dbo.Models_PriModels ON dbo.Models.PriModel = dbo.Models_PriModels.ID AND   
    dbo.Models_OEMs.ID = dbo.Models_PriModels.Manufacturer INNER JOIN
    dbo.Models_SecModels ON dbo.Models.SecModel = dbo.Models_SecModels.ID AND 
    dbo.Models_PriModels.ID = dbo.Models_SecModels.PriModel ORDER BY dbo.Models.ID 
    DESC"
    UpdateCommand="Procedure_UpdateModelsTables" UpdateCommandType="StoredProcedure"
    OnUpdated="OnDSUpdatedHandler"   
    InsertCommand="">
</asp:SqlDataSource>

    CREATE PROCEDURE <Procedure_UpdateModelsTables> 

    <@Param int>,
    <@ManufacturerID int>,
    <@Manufacturer varchar(50)>,
    <@PriModelID int>,
    <@PriModel varchar(50)>,
    <@SecModelID int>,
    <@SecModel varchar(50)>,
    <@FormID int>,
    <@Form varchar(50)>,
    <@ModelsID int>,
    <@Active char(1)>,
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    -- Insert statements for procedure here
    --SELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2>
    IF @param = 1     
        exec SP_OEMTable @ManufacturerID,@Manufacturer;
    IF @param = 2
       exec SP_PriModelsTable @PriModelID,@PriModel;
    IF @param = 3     
        exec SP_SecModelsTable @SecModelID,@SecModel;
    IF @param = 4
       exec SP_FormsTable @FormID,@Form;
    IF @param = 5
       exec SP_ModelsTable @ModelsID,@Active;

END

CREATE PROCEDURE SP_OEMTable
    @ManufacturerID int,
    @Manufacturer varchar(50)
AS
BEGIN  
    UPDATE Models_OEMs SET Manufacturer = @Manufacturer WHERE ID = @ManufacturerID;
END

CREATE PROCEDURE SP_PriModelsTable
    @PriModelID int,
    @ManufacturerID int,
    @PriModel varchar(50)
AS
BEGIN  
    UPDATE Models_PriModels SET PriModel = @PriModel WHERE ID = @PriModelID AND Manufacturer = @ManufacturerID;
END
CREATE PROCEDURE SP_SecModelsTable
    @SecModelID int,
    @PriModelID int,
    @SecModel varchar(50)
AS
BEGIN  
    UPDATE Models_SecModels SET SecModel = @SecModel WHERE ID = @SecModelID AND PriModel = @PriModelID;
END
CREATE PROCEDURE SP_FormsTable
    @FormID int,
    @Form varchar(50)
AS
BEGIN  
    UPDATE Models_FormFactor SET Form = @Form WHERE ID = @FormID;
END
CREATE PROCEDURE SP_ModelsTable
    @ModelsID int,
    @Active char(1)
AS
BEGIN  
    UPDATE Models SET Active = @Active WHERE ID = @ModelsID;
END
GO
4

1 回答 1

0

本教程是我用来学习此过程的内容:

http://www.codeproject.com/Articles/24085/Insert-Update-Delete-with-Gridview-Simple-Way

于 2013-04-02T17:20:42.243 回答