将动态参数绑定到不工作的SQL数据源

本文关键字:工作 SQL 数据源 动态 参数 绑定 | 更新日期: 2023-09-27 18:01:32

我想通过使用SqlDataSource调用子gridview上的存储过程。

这就是我正在做的:

<obout:Grid runat="server" ID="grid1" AutoGenerateColumns="false" PageSize="10" FolderStyle="~/Styles/Grid/premiere_blue"
       AllowAddingRecords="true" Width="100%">
    <Columns>
        <obout:CheckBoxSelectColumn ShowHeaderCheckBox="true" ControlType="Standard" Visible="false">
        </obout:CheckBoxSelectColumn>
        <obout:Column ID="Column1" DataField="mkey" HeaderText="mkey" Width="8%" runat="server" Wrap="true" Visible="true">
        </obout:Column>
        <obout:Column DataField="Doc_No" Visible="true" HeaderText="I/W No" Width="12%" runat="server" Wrap="true">
        </obout:Column>
        <obout:Column DataField="DocType" Visible="true" HeaderText="Doc Type" Width="8%" runat="server" Wrap="true">
        </obout:Column>
        <obout:Column Visible="false" DataField="Party_Mkey" HeaderText="Party Mkey" Width="0" runat="server" Wrap="true">
        </obout:Column>
        <obout:Column Visible="true" DataField="Party_Name" HeaderText="Party Name" Width="10%" runat="server" Wrap="true">
        </obout:Column>
        <obout:Column Visible="true" DataField="LastAction_DateTime" HeaderText="Last Action Date" Width="8%" runat="server" Wrap="true">
        </obout:Column>
        <obout:Column Visible="false" DataField="Status_Flag" HeaderText="Status Flag" Width="0" runat="server" Wrap="true">
        </obout:Column>
        <obout:Column Visible="true" DataField="CurrStatus" HeaderText="Curr Status" Width="8%" runat="server" Wrap="true">
        </obout:Column>
        <obout:Column Visible="false" DataField="To_Department" HeaderText="Department_Id" Width="0" runat="server" Wrap="true">
        </obout:Column>
        <obout:Column Visible="true" DataField="Type_Desc" HeaderText="Resp Dept" Width="10%" runat="server" Wrap="true">
        </obout:Column>
        <obout:Column Visible="false" DataField="To_User" HeaderText="User_Id" Width="0" runat="server" Wrap="true">
        </obout:Column>
        <obout:Column Visible="true" DataField="UserName" HeaderText="Resp User" Width="8%" runat="server" Wrap="true">
        </obout:Column>
        <obout:Column Visible="true" DataField="No_Of_Days" HeaderText="No Of Days" Width="10%" runat="server" Wrap="true">
        </obout:Column>
        <obout:Column DataField="Status" HeaderText="Status" Visible="true" Width="10%" runat="server" Wrap="true">
            <TemplateSettings TemplateId="tplStatusWeb" />
        </obout:Column>
        <obout:Column DataField="View" HeaderText="View" Visible="true" Width="5%" runat="server" Wrap="true">
             <TemplateSettings TemplateId="tplViewWeb" />
        </obout:Column>
    </Columns>
    <Templates>
            <obout:GridTemplate runat="server" ID="tplStatusWeb">
                <Template>
                    <a href="#" runat="server" id="txtStatus"></a>
                </Template>
            </obout:GridTemplate>
            <obout:GridTemplate runat="server" ID="tplViewWeb">
                <Template>
                    <a href="#" runat="server" id="txtView"></a>
                </Template>
            </obout:GridTemplate>
        </Templates>
        <%--Child grid--%>
        <MasterDetailSettings LoadingMode="OnCallback" ShowEmptyDetails="true" />
        <DetailGrids>
            <obout:DetailGrid runat="server" ID="grid2" Width="80%" AutoGenerateColumns="false"
                AllowAddingRecords="false" ShowFooter="true" PageSize="5" DataSourceID="sqldt2"
                FolderStyle="~/Styles/Grid/premiere_blue" ForeignKeys="mkey">
                <ClientSideEvents ExposeSender="true" OnClientPopulateControls="onPopulateControls" />
                <Columns>
                    <obout:Column ID="mkey" DataField="mkey" HeaderText="mkey" Width="15%" runat="server"
                        Wrap="true" Visible="true">
                    </obout:Column>
                    <obout:Column DataField="NextStatus" HeaderText="Next Status" Width="10%" runat="server"
                        Wrap="true" Visible="true">
                    </obout:Column>
                    <obout:Column DataField="Name" HeaderText="Name" Width="10%" runat="server" Wrap="true"
                        Visible="true">
                    </obout:Column>
                    <obout:Column DataField="Type_Desc" HeaderText="Description" Width="10%" runat="server"
                        Wrap="true" Visible="true">
                    </obout:Column>
                    <obout:Column DataField="No_of_Days" HeaderText="No of Days" Width="5%" runat="server"
                        Wrap="true" Visible="true">
                    </obout:Column>
                    <obout:Column DataField="CUser_ID" HeaderText="User Id" Width="10%" runat="server"
                        Wrap="true" Visible="true">
                    </obout:Column>
                    <obout:Column DataField="CDept_ID" HeaderText="Dept Id" Width="10%" runat="server"
                        Wrap="true" Visible="true">
                    </obout:Column>
                    <obout:Column DataField="NStatus_flag" HeaderText="Status flag" Width="10%" runat="server"
                        Wrap="true" Visible="true">
                    </obout:Column>
                    <obout:Column DataField="Remarks" HeaderText="Remarks" Width="15%" runat="server"
                        Wrap="true" Visible="true">
                    </obout:Column>
                </Columns>
            </obout:DetailGrid>
        </DetailGrids>
    </obout:Grid>

另外,我使用SqlDataSource这样:

<asp:SqlDataSource runat="server" ID="sqldt2" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
     ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
     SelectCommand="Sp_Inward_Hdr_Trl_Link_trl"
     SelectCommandType="StoredProcedure">
     <SelectParameters>
         <asp:Parameter Name="StrPriCondition" Type="String" DefaultValue=" and IH.To_Department in (1053) and IH.Last_To_User =154" />
         <asp:Parameter Name="StrPriCondition2" Type="String" DefaultValue=" and IH.To_Department in (1053) and IH.To_User=154" />
         <asp:Parameter Name="StrPriCondition3" Type="String" DefaultValue=" and T1.dept_Mkey in (1053) and U.mkey=154" />
     </SelectParameters>
</asp:SqlDataSource> 

目前,我为SqlDataSource中的每个参数提供默认值。

我想将这3个参数动态地绑定到子gridview。

如何动态绑定?请建议。

根据naveen的建议,我尝试了RowDataBound。

protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {   
        string gridId = Grid1.DataKeys[e.Row.RowIndex].Value.ToString();
        GridView grid2 = e.Row.FindControl("Grid2") as GridView;
        /*grid2.DataSource = (string.Format("select top 3 * from Orders where CustomerId='{0}'", gridId)); // Bind gridview with SP ?
        grid2.DataBind(); */
    }
}

将动态参数绑定到不工作的SQL数据源

var prmParameter = new Parameter();
prmParameter .Name = "StrPriCondition";
prmParameter .Type = TypeCode.String;
prmParameter .DefaultValue = " and IH.To_Department in (1053) and IH.Last_To_User =154";
sqldt2.SelectParameters.Add(prmParameter );

以这种方式更新c#中的参数值

    string StrPriCondition = sqldt2.SelectParameters["StrPriCondition"].DefaultValue.ToString();
     string StrPriCondition2 = sqldt2.SelectParameters["StrPriCondition2"].DefaultValue.ToString();
   string StrPriCondition3 =  sqldt2.SelectParameters["StrPriCondition2"].DefaultValue.ToString();