如何使用动态绑定在AspxGridView中执行页脚摘要和组摘要?

时间:2016-12-20 13:21:14

标签: c# gridview devexpress aspxgridview

我制作了一个使用AspxGridView显示记录的代码。现在我想显示页脚和分组的总和(Column_Name),count(Column_Name)。

这是我的代码:

<dx:ASPxGridView ID="ASPxGridView1" runat="server" >
                    <SettingsPager PageSize="50">
                    </SettingsPager>
                    <Settings ShowFilterRow="True" ShowGroupPanel="True" ShowFooter="True" ShowGroupFooter="VisibleIfExpanded" />
                    <SettingsCommandButton>
                        <ShowAdaptiveDetailButton ButtonType="Image"></ShowAdaptiveDetailButton>

                        <HideAdaptiveDetailButton ButtonType="Image"></HideAdaptiveDetailButton>
                    </SettingsCommandButton>
                    <SettingsDataSecurity AllowDelete="False" AllowEdit="False" AllowInsert="False" />
                    <SettingsSearchPanel Visible="True" />
                </dx:ASPxGridView>

C#代码:

protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
        {
            ASPxGridView1.Visible = true;
            string cs = ConfigurationManager.ConnectionStrings["HQMatajerConnectionString"].ConnectionString;
            whereQuery = getWhereQuery();
            using (SqlConnection con = new SqlConnection(cs))
            {
                string querysss = "select   "+txtSelectedColumn.Text+ @"
                            FROM        [HQMatajer].[dbo].[Transaction] as transactions
                            RIGHT JOIN  [HQMatajer].[dbo].[TransactionEntry] as transactionsEntry
                            ON transactions.TransactionNumber=transactionsEntry.TransactionNumber 
                            INNER JOIN  [HQMatajer].[dbo].[Item] as items
                            ON transactionsEntry.ItemID=items.ID
                            INNER JOIN  [HQMatajer].[dbo].[Supplier] as suppliers
                            ON items.SupplierID=suppliers.ID
                            LEFT JOIN [HQMatajer].[dbo].[Department] as departments
                            ON departments.ID=items.DepartmentID
                            LEFT JOIN [HQMatajer].[dbo].[Category] as categories
                            ON categories.ID=items.CategoryID
                            where  " + txtQuery.Text;

                SqlCommand cmd = new SqlCommand();

                cmd.Connection = con;
                cmd.CommandText = querysss;
                con.Open();

                SqlDataAdapter sda = new SqlDataAdapter(cmd);

                sda.Fill(ds);

                ASPxGridView1.AutoGenerateColumns = true;
                ASPxGridView1.DataSource = ds;
                ASPxGridView1.DataBind();
            }
        }

你可以注意到我的sql查询语句。在该用户中,仅决定他们想要显示哪一列。如果他们在select语句中选择Price column_name。如果用户进行分组,它必须在页脚中显示所有记录的总和(价格)以及分组

1 个答案:

答案 0 :(得分:0)

您可以在c#代码中添加摘要项,并在每次回发中更改字段名称。例如页脚摘要

ASPxSummaryItem gs = new ASPxSummaryItem();
gs.FieldName = txtSelectedColumn.Text;
gs.SummaryType = DevExpress.Data.SummaryItemType.Sum;
this.ASPxGridView1.TotalSummary.Add(gs);

如果字段名称没有更改,您可以删除旧摘要并每次在所需列添加新摘要。您也可以为每个组摘要执行相同的操作。您还可以查看https://documentation.devexpress.com/#AspNet/DevExpressWebASPxGridView_GroupSummarytopichttps://documentation.devexpress.com/#AspNet/DevExpressWebASPxGridView_TotalSummarytopic