Gridview项目模板将值从外部网格传递到内部网格

本文关键字:网格 内部 从外部 项目 Gridview | 更新日期: 2023-09-27 18:14:56

我试图嵌套两个Grid_Views像…

 <asp:GridView ID="gv" runat="server" CellPadding="0" ForeColor="#333333" 
        GridLines="None" AutoGenerateColumns="False" Width="100%" 
        DataSourceID="groupby" OnRowDataBound="RowBound">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="innerrecs">
                        <Columns>
                            <asp:BoundField DataField="date" HeaderText="date" ReadOnly="True" 
                                SortExpression="date" />
                            <asp:BoundField DataField="mat_no" HeaderText="mat_no" 
                                SortExpression="mat_no" />
                            <asp:BoundField DataField="injur" HeaderText="injur" SortExpression="injur" />
                            <asp:BoundField DataField="clm bal" HeaderText="clm bal" 
                                SortExpression="clm bal" />
                            <asp:BoundField DataField="DOA" HeaderText="DOA" SortExpression="DOA" />
                            <asp:BoundField DataField="county" HeaderText="county" ReadOnly="True" 
                                SortExpression="county" />
                        </Columns>

                    </asp:GridView>
                    <asp:SqlDataSource ID="innerrecs" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:TimeMatters10ConnectionString %>" SelectCommand="SELECT CONVERT(CHAR,DATEADD(D, [event].[date], '1800-12-28'),101) as 'date', 
                            [event].mat_no, matter.mat1_01_07 as 'injur', matter.mat1_07_01 as 'clm bal', MAT1_03_08 as 'DOA',replace(matter.mat1_05_03, 'COUNTY OF ', '') as 'county'
                            FROM lntmuser.[event]
                            INNER JOIN lntmuser.matter
                            ON lntmuser.matter.sysid = lntmuser.[event].mat_id
                            INNER JOIN lntmuser.matter2
                            ON lntmuser.matter2.sysid = lntmuser.[event].mat_id
                            WHERE DateDiff(month, Convert(date, CONVERT(CHAR,DATEADD(D, [event].[date], '1800-12-28'),101), 101), GETDATE()) = 1
                            AND event.con_no = @conno
                            order by matter.client">
                        <SelectParameters>
                            <asp:SessionParameter Name="conno" SessionField="@conno" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                If You have Any Questions in Reference to this matter please do not hesitate ot contact me.<br /><br />
                Thank You,<br />
                Dara
            <br clear="all" style="page-break-before:always" />
            </ItemTemplate>
        </asp:TemplateField>                
    </Columns>
</asp:GridView>
    <asp:SqlDataSource ID="groupby" runat="server" 
        ConnectionString="<%$ ConnectionStrings:TimeMatters10ConnectionString %>" SelectCommand="SELECT MAX(event.[con_no]) as 'con_no', replace(replace(replace([matter].client, 'P.C.', ''), 'PC', ''), 'INC', '') as 'client',
        MAX(mat1_02_02) as 'BillSup', MAX(mat1_02_09) as 'Fax'
        , CONVERT(char, GETDATE(), 101) as 'DT'
        FROM lntmuser.[event]
        INNER JOIN lntmuser.matter
        ON lntmuser.matter.sysid = lntmuser.[event].mat_id
        INNER JOIN lntmuser.matter2
        ON lntmuser.matter2.sysid = lntmuser.[event].mat_id
        WHERE DateDiff(month, Convert(date, CONVERT(CHAR,DATEADD(D, [event].[date], '1800-12-28'),101), 101), GETDATE()) = 1
        GROUP BY replace(replace(replace([matter].client, 'P.C.', ''), 'PC', ''), 'INC', '')">
    </asp:SqlDataSource>

基本上,我想将值"conno"从外部网格传递到内部网格。I tried

protected void RowBound(object sender, GridViewRowEventArgs e)
{
    //string conno = ((DataBoundLiteralControl)e.Row.Cells[0].Controls[1]).Text;
    //string conno = DataBinder.Eval(e.Row.DataItem, "con_no").ToString();
    Session["conno"] = conno;
}

但这是不好的,正确的方法是什么?由于

Gridview项目模板将值从外部网格传递到内部网格

尝试以下方法(未测试):将DataKeyNames属性添加到外部gridView并将其设置为con_no值。将嵌套sqldatasource的参数从SessionParameter改为parameter,并重写RowBound方法,如下所示:

protected void RowBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var innerrecs = e.Row.FindControl("innerrecs") as SqlDataSource;
        innerrecs.SelectParameters["conno"].DefaultValue = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
        var gridView1 = e.Row.FindControl("GridView1") as GridView;
        gridView1.DataBind();
    }
}