【问题标题】:Calculate row in GridView based off two other rows根据另外两行计算 GridView 中的行
【发布时间】:2016-08-12 18:54:51
【问题描述】:

我有一个 GridView,它显示了名为 XDA 的作业数和文档数。在名为 EPOD 的第三列中,我想通过作业减去 XDA 来计算。

网格视图:

<asp:GridView runat="server" 
                                id="gvResults" 
                                CssClass="tblResults" 
                                OnItemDataBound="gvResults_OnItemDataBound" 
                                AllowSorting="true" 
                                OnSortCommand="gvResults_Sort" 
                                DataKeyField="ID" 
                                AutoGenerateColumns="false">
                            <HeaderStyle CssClass="tblResultsHeader" />
                            <AlternatingRowStyle BackColor="#EEEEEE" />
                                <Columns>
                                    <asp:BoundField DataField="JobsDelivered" HeaderText="No. Jobs deliv" SortExpression="JobsDelivered"></asp:BoundField>
                                    <asp:BoundField DataField="ePODXDA" HeaderText="ePOD XDA" SortExpression="ePODXDA"></asp:BoundField>

后面的代码:

 protected void gvResults_OnItemDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LookupLeagueTable main = (LookupLeagueTable)e.Row.DataItem;
            GridView gv = (GridView)sender;

        }
    }

所以我尝试使用它,但代码卡在 foreach 中:

foreach (DataRow row in e.Row.Cells)
{
    row["EPOD"] = Convert.ToInt32(row["JobsDelivered"]) - Convert.ToInt32(row["ePODXDA"]);
}

我也试过main.EPOD = main.JobsDelivered - main.ePODXDA;,但网格仍然显示为零

填充网格:

List<LookupLeagueTable> main = LookupLeagueTable.SearchCustomerListItems(Company.Current.CompanyID,
                    SearchStrings,
                    SearchFields,
                    DateTypeKey,
                    FromDate,
                    ToDate,
                    SortExpression,
                    SortDirection);



        gvResults.DataSource = main;
        gvResults.DataBind();

SearchCustomerListItems然后运行一条sql语句:

select 
        sum(case when (select count(distinct j.ID) from job_new j where jn.ID = j.ID AND j.JobOwnerID = j.DeliverMemberID)>0 then 1 else 0 end) as JobsDelivered,
        sum(case when (select count(jds.EPODImage) from job_dockets jds where jds.jobid=jn.id )>0 then 1 else 0 end) as ePODXDA
    FROM customer c
    LEFT JOIN job_address ja ON c.AccountCode = ja.Code AND c.Company_ID = ja.Company_ID
    JOIN  AddressType jat ON ja.AddressType = jat.ID and jat.Description = 'Debtor'
    LEFT JOIN job_new jn ON ja.JobID = jn.ID
    WHERE  jn.IsActive = 1 

【问题讨论】:

  • 如何填充网格?
  • @Mainak 我编辑了我的问题以显示代码

标签: c# asp.net gridview row cells


【解决方案1】:

您可以通过多种方式进行调整。试试这个简单的代码可能会对你有所帮助:

添加一个模板字段代替第三列:

<asp:TemplateField HeaderText="EPOD">
 <ItemTemplate>
  <asp:Lable runat="server" ID="lblEpod">
 </ItemTemplate>
</asp:TemplateField>

然后 OnRowDataBound 你可以按预期计算字段:

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        int no_job = Convert.ToInt32(e.Row.Cells[0].Text); // as your No of Job is at 0 place of Gridview
        int XDA = Convert.ToInt32(e.Row.Cells[1].Text); 
        Lable lblEpod = e.Row.FindControl("lblEpod") as Lable;
        lblEpod.Text = Convert.ToString(no_job - XDA);
    }
}

【讨论】:

    猜你喜欢
    • 2019-08-02
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    相关资源
    最近更新 更多