【问题标题】:Trying To Display Average by Dividing Each Column by the Sum of the Same Column in a GridView with ASP.Net尝试通过使用 ASP.Net 将每列除以 GridView 中同一列的总和来显示平均值
【发布时间】:2021-09-21 21:25:42
【问题描述】:

我需要在已经格式化为显示索引和点击的 Gridview 中以百分比显示平均点击次数。我需要将每个“点击次数”列中的数字除以同一“点击次数”列的总和。

Index   Clicks   %
       
| 1 |  | 24 | | 44% |          
| 2 |  | 31 | | 56% | 

这是我的 GridView

<asp:Table ID="Table2" runat="server">
    <asp:TableRow>
        <asp:TableCell>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
                   <Columns>
                       <asp:BoundField DataField="Index" HeaderText="Index" SortExpression="Index"></asp:BoundField>
                       <asp:BoundField DataField="Clicks" HeaderText="Clicks" ReadOnly="True" SortExpression="Clicks"></asp:BoundField>
                     
                   </Columns>
               </asp:GridView>
               
               <asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:trackingstatsConnectionString %>' SelectCommand="SELECT LinkId as 'Index', count(*) as Clicks FROM Track_Click_7 where CampaignId = 18444 Group by LinkId;"></asp:SqlDataSource>
           </asp:TableCell>
       </asp:TableRow>
   </asp:Table>

   

【问题讨论】:

    标签: c# sql asp.net gridview


    【解决方案1】:

    您可以通过多种不同的方式做到这一点。

    但是,简单的方法?

    在页面类级别简单地声明一个 PUBLIC var,然后将其用作标记中的表达式。

    因此,加载网格的典型代码如下所示:

        public double TotalClicks = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadGrid();
        }
    
        void LoadGrid()
        {
            // load up our grid
            using (SqlCommand cmdSQL = new SqlCommand("SELECT TOP 15 * from tblHotels ORDER BY HotelName ",
                new SqlConnection(Properties.Settings.Default.TEST4)))
            {
                cmdSQL.Connection.Open();
                DataTable rst = new DataTable();
                rst.Load(cmdSQL.ExecuteReader());
                TotalClicks = Convert.ToDouble(rst.Compute("SUM(Clicks)", ""));
                GridView1.DataSource = rst;
                GridView1.DataBind();
            }
        }
    

    所以,现在当您加载表格时,只需将该列总计 - 设置为该页面的公共 var。

    现在,您可以像这样在网格标记中简单地显示 %:

    <asp:TemplateField HeaderText ="%">
      <ItemTemplate>
       <asp:Label ID="MyPercent" runat="server" 
        Text = '<%#  String.Format("{0:P2}.", (Convert.ToDouble(Eval("Clicks")) / TotalClicks )) %>'   >
       </asp:Label>
      </ItemTemplate>
    </asp:TemplateField>
    

    所以总网格标记如下所示:

    <asp:GridView ID="GridView1" runat="server" CssClass="table table-hover" 
        AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" >
    
        <Columns>
            <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="LastName" />
            <asp:BoundField DataField="HotelName" HeaderText="HotelName"  />
            <asp:BoundField DataField="Province" HeaderText="Province" />
            <asp:BoundField DataField="Clicks" HeaderText="Clicks"  />
    
            <asp:TemplateField HeaderText ="%">
                <ItemTemplate>
                    <asp:Label ID="MyPercent" runat="server" 
                        Text = '<%#  String.Format("{0:P2}.", (Convert.ToDouble(Eval("Clicks")) / TotalClicks )) %>'   >
                    </asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    

    输出:

    【讨论】:

      【解决方案2】:
      SELECT DISTINCT
          LinkId [Index],
          Count(*) over (Partition by LinkId) [Clicks],
          Convert(VARCHAR(10), count(*) over (Partition by LinkId) * 100 / count(*) over (Partition by 1)) + '%' [Percent]
      FROM
          Track_Click_7
      WHERE
          CampaignId = 18444
      

      我会首先尝试在 SQL 语句中解决,就像上面一样。虽然可能不是最好的解决方案,但一个想法。

      【讨论】:

      • 完美!谢谢!
      猜你喜欢
      • 2023-03-13
      • 2018-11-02
      • 1970-01-01
      • 2023-01-27
      • 1970-01-01
      • 2018-01-29
      • 1970-01-01
      • 1970-01-01
      • 2023-02-10
      相关资源
      最近更新 更多