C# - 将数据从存储过程动态加载到 gridview

C# - Loading data from stored procedure to gridview dynamically

我正在使用存储过程在我的 gridview 中加载数据。根据用户选择,他/她应该能够在 asp.net gridview 中查看数据,可以是一个位置所有租户的详细销售数据,也可以是该位置的总销售额。

这意味着更改字段列。

每个细节:

tenantcode 租户名称、位置、总销售额

每个位置的总计:

位置,总销售额(使用 group by 子句实现)

IN STORED PROCEDURE ,当然一切正常,但在 asp.net 中,每个详细选项都有效,但是当我选择下一个选项,即每个位置的所有销售额时,它会产生此错误:

A field or property with the name 'tenantcode' was not found on the selected data source.

标记:

<asp:GridView ID="grdMarketingReport1" runat="server" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical" Font-Size="Smaller" EmptyDataText="No Records Found" ShowHeaderWhenEmpty="True" Width="100%" AutoGenerateColumns="false">
    <EmptyDataRowStyle BackColor="white" ForeColor="black" />
    <EmptyDataTemplate>No Data Found.</EmptyDataTemplate>
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:BoundField HeaderText="RP Code" DataField="tenantcode"  />
        <asp:BoundField HeaderText="Retail Partner" DataField="name" />
        <asp:BoundField HeaderText="Location" DataField="locationd" />
        <asp:BoundField HeaderText="Gross Sales" DataField="gs"  />
    </Columns>
    <FooterStyle BackColor="#CCCC99" />
    <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" Height="25px" />
    <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
    <RowStyle BackColor="#F7F7DE" />
    <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#FBFBF2" />
    <SortedAscendingHeaderStyle BackColor="#848384" />
    <SortedDescendingCellStyle BackColor="#EAEAD3" />
    <SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>

我实际上正在考虑创建两个不同的 gridview 来满足我的需要,但这是最少的选择,除非它是唯一的方法。能否仅在一个 GRIDVIEW 中完成?

存储过程(实际 SP 的一部分)

if (@RP = 'ALL' and @Location = 'ALL' and @Business = 'ALL')
begin




        --step 1: get the sales per date    
           SELECT a.tenantcode ,  b.name , a.location , c.locationd , d.[desc] as 'Business' , a.date , a.gsc, b.sdate , b.cdate 
           into #Sample5
           FROM DAILYMOD a INNER JOIN TENANT b on a.tenantcode = b.tenantcode 
                           INNER JOIN LOCATION c on b.location = c.location 
                           INNER JOIN BUSINESS d on b.business = d.code 

              WHERE ((a.date between @DateFrom1  and @DateTo1 )
                  or (a.date between @DateFrom2  and @DateTo2 )
                  or (a.date between @dateFromLY  and @dateToLY))


           ORDER BY a.location , a.tenantcode , a.date



         --step 2: group the gsc per date ranges
           select tenantcode , name, location,  locationd, business , sdate, cdate,

           SUM(case when date between @DateFrom1  and @DateTo1  then [gsc] else 0 end) 'date1',
           SUM(case when date between @DateFrom2  and @DateTo2   then [gsc] else 0 end) 'date2',
           SUM(case when date between @dateFromLY  and @dateToLY then [gsc] else 0 end) 'dateLY'
           from #Sample5
           GROUP BY tenantcode , name, location, locationd , business, sdate, cdate





end




else
begin

        --step 1: get the sales per date    
           SELECT a.tenantcode ,  b.name , a.location , c.locationd , d.[desc] as 'Business' , a.date , a.gsc, b.sdate , b.cdate 
           into #Sample7
           FROM DAILYMOD a INNER JOIN TENANT b on a.tenantcode = b.tenantcode 
                           INNER JOIN LOCATION c on b.location = c.location 
                           INNER JOIN BUSINESS d on b.business = d.code 

              WHERE ((a.date between @DateFrom1  and @DateTo1  )
                  or (a.date between @DateFrom2  and @DateTo2 )
                  or (a.date between @dateFromLY  and @dateToLY ))


           ORDER BY a.location , a.tenantcode , a.date



         --step 2: group the gsc per date ranges
           select  location, locationd, 

            SUM(case when date between @DateFrom1  and @DateTo1  then [gsc] else 0 end) 'date1',
           SUM(case when date between  @DateFrom2  and @DateTo2  then [gsc] else 0 end) 'date2',
           SUM(case when date between @dateFromLY  and @dateToLY then [gsc] else 0 end) 'dateLY'
           from #Sample7
           GROUP BY  location, locationd 




end

在您的存储过程中 else 第 2 部分没有 return tenantcode

  --step 2: group the gsc per date ranges
           select  location, locationd, 

            SUM(case when date between @DateFrom1  and @DateTo1  then [gsc] else 0 end) 'date1',
           SUM(case when date between  @DateFrom2  and @DateTo2  then [gsc] else 0 end) 'date2',
           SUM(case when date between @dateFromLY  and @dateToLY then [gsc] else 0 end) 'dateLY'
           from #Sample7
           GROUP BY  location, locationd 

也尝试 return 该列并检查它。

更新:

--step 2: group the gsc per date ranges
               select '' AS tenantcode ,location, locationd, 

                SUM(case when date between @DateFrom1  and @DateTo1  then [gsc] else 0 end) 'date1',
               SUM(case when date between  @DateFrom2  and @DateTo2  then [gsc] else 0 end) 'date2',
               SUM(case when date between @dateFromLY  and @dateToLY then [gsc] else 0 end) 'dateLY'
               from #Sample7
               GROUP BY  location, locationd