【问题标题】:C# - Loading data from stored procedure to gridview dynamicallyC# - 将数据从存储过程动态加载到gridview
【发布时间】:2015-03-11 04:25:39
【问题描述】:

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

这意味着更改字段列。

对于每个细节:

租户代码租户名称、位置、总销售额

每个位置的 TOTAL:

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

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

我实际上正在考虑创建两个不同的网格视图来满足我的需求,但这是最少的选择,除非它是唯一的方法。只能在一个网格视图中完成吗?

存储过程(实际 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

【问题讨论】:

  • 嗨!真的有必要大喊大叫吗?并且还包括整个 gridview 标记,这里的所有内容都与问题有关吗?
  • 我想是的,为问题提供详尽的解释。
  • 您实际上不需要样式,只需要列。我会删除所有不相关的内容,以帮助读者专注于绑定问题。
  • 你的 sp,它的底层查询是什么?什么是输出,它的字段?
  • 谢谢。好的,我会发布我的 sp 以供参考

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


【解决方案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 

【讨论】:

  • 有意分组或获取一个位置的总销售额。请查看表格图片。
  • 是的,但是在您的网格视图中,您对此列有参考,而数据源没有此列,这就是它抛出异常的原因。 @rickyProgrammer
  • 您可以在您的 select 子句和 groupby 子句中添加“tenantcode”。它仍然会对其进行分组。 @rickyProgrammer
  • 是的,但结果不是我想要达到的,当我在 select 子句中包含租户代码时,也在 group by 子句中,输出仍将是每个租户的详细信息,而不是每个租户的合并销售额地点。 (就像我上面说明的那样)在表格图像中
  • ok.. 你能通过替换你的查询来尝试我的更新查询吗? @rickyProgrammer
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-16
相关资源
最近更新 更多