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

时间:2015-03-11 04:25:40

标签: c# asp.net sql-server stored-procedures gridview

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

这意味着更改字段列。

对于PER DETAIL:

租户代码租户名称,地点,总销售额

enter image description here

对于每个位置的总计:

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

enter image description here

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

  

名称为' tenantcode'的字段或媒体资源未在所选数据源中找到。

标记:

<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中完成吗?

STORED PROC (实际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

1 个答案:

答案 0 :(得分:1)

在您的商店程序中else部分第2步不会返回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 

尝试返回该列并检查它。

更新:

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