基于另外两行

时间:2016-04-20 08:26:30

标签: c# asp.net gridview row cells

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

GridView的:

<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 

1 个答案:

答案 0 :(得分:0)

有许多方法可以适应。试试这个简单的代码可以帮到你:

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

<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);
    }
}