合并Templatefield与Boundfield

时间:2014-12-24 05:53:25

标签: c# asp.net .net gridview merge

我有这个行字段,它包含前一行的相同值。我可以将它与使用boundField合并,但当我将其更改为templateField时,我的代码中的最后一行并没有像我想的那样工作。所以我使用绑定字段(将最后一行更改为boundfield)进行比较,并且它有效。我使用templatefield的原因是因为我想使用linkbutton commandArgument,所以它可以使用session将选择的数据传递到另一个页面。所以我的问题是,为什么合并不能很好地与模板字段一起工作?但是它的界限很好吗?我仍然想使用模板字段,因为链接按钮。但是,如果有人可以给我一个例子来使用带有链接的绑定字段,我就可以使用它。

这是我的aspx代码。

<asp:GridView ID="GridViewApproval" AutoGenerateColumns="false" runat="server" CssClass="Grid" AllowPaging="true"       
    Width="100%" OnDataBound="OnDataBound" ShowHeaderWhenEmpty="true" EmptyDataText="No Data Found">

     <Columns>

        <asp:TemplateField HeaderText="No.">
            <ItemTemplate>
                <asp:Label ID="lblNo" runat="server"
                    Text = '<%# Container.DataItemIndex + 1 %>'> </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

         <asp:BoundField DataField="EMPLOYEE_ID" HeaderText="Emp ID"/>
         <asp:BoundField DataField="EMPLOYEE_NAME" HeaderText="Emp Name"/>
         <asp:BoundField DataField="POSITION_DESCRIPTION" HeaderText="Position"/>
         <asp:BoundField DataField="SECTION_DESCRIPTION" HeaderText="Section"/>
         <asp:BoundField DataField="PROPOSED_TOPIC" HeaderText="Topic"/>
         <asp:BoundField DataField="METHOD_DEV_NAME" HeaderText="Method of Development"/>
         <asp:BoundField DataField="ESTIMATE_COST" HeaderText="Cost Estimation(USD)" />
         <asp:BoundField DataField="ROD_DESC" HeaderText="Reason of Dev"/>
         <asp:BoundField DataField="SCHEDULE_PLAN" HeaderText="Schedule Plan"/>
         <asp:BoundField DataField="ILP_CODE" HeaderText = "CODE" />

         <asp:TemplateField HeaderText="CODE">
            <ItemTemplate>
            <asp:LinkButton ID="linkAction" runat="server"
                CommandArgument = '<%# Eval("ILP_CODE")%>'                   
                Text = '<%# Bind("ILP_CODE")%>' OnClick = "ActionApproval"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>

         </Columns>

    </asp:GridView>

这是我的aspx.cs代码

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack & Request.IsAuthenticated)
        {
            BindData();
        }
    }



    private void BindData()
    {
        String magicNumber = Server.HtmlEncode(Request.Cookies[Constant.CookiesEmployeeId].Value);
        decimal nextAppEmpId = decimal.Parse(magicNumber);
        GridViewApproval.DataSource = (nextAppEmpId.Equals("null") || nextAppEmpId == 0) ? apDetail.GetIlpProcessPosition() : apEntity.getIlpProcessPositionNextEmpId(nextAppEmpId);

        GridViewApproval.DataBind();
    }

    protected void ActionApproval(object sender, EventArgs e)
    {
        LinkButton linkAction = (LinkButton)sender;
        Session["ILP_CODE"] = linkAction.CommandArgument;
        Response.Redirect("~/Web/ILP/ApprovalListDetail.aspx");
    }

    protected void OnDataBound(object sender, EventArgs e)
    {
        for (int i = GridViewApproval.Rows.Count - 1; i > 0; i--)
        {
            GridViewRow row = GridViewApproval.Rows[i];
            GridViewRow previousRow = GridViewApproval.Rows[i - 1];
            for (int j = 0; j < row.Cells.Count; j++)
            {
                if (j != 0)
                {
                if (row.Cells[j].Text == previousRow.Cells[j].Text)
                {
                    if (previousRow.Cells[j].RowSpan == 0)
                    {
                        if (row.Cells[j].RowSpan == 0)
                        {
                            previousRow.Cells[j].RowSpan += 2;
                        }
                        else
                        {
                            previousRow.Cells[j].RowSpan = row.Cells[j].RowSpan + 1;
                        }
                        row.Cells[j].Visible = false;
                    }
                }
                }

            }
        }

        string ilpCode = null, ilpCodeSave = null;

        for (int count = 0; count < GridViewApproval.Rows.Count; count++)
        {
            ilpCode = GridViewApproval.Rows[count].Cells[10].Text;
            if (!ilpCode.Equals(ilpCodeSave))
            {
                //GridViewApproval.Rows[count].Cells[10].Text = "Details";
            }
            ilpCodeSave = GridViewApproval.Rows[count].Cells[10].Text;

        }
    }

这是我编译时的图像 BoundVSTemplate

1 个答案:

答案 0 :(得分:1)

这是我能做的: 享受

//looping for boundfield
    for (int i = GridViewApproval.Rows.Count - 1; i > 0; i--)
        {
            GridViewRow row = GridViewApproval.Rows[i];
            GridViewRow previousRow = GridViewApproval.Rows[i - 1];
            for (int j = 0; j < row.Cells.Count & j != 5; j++)
            {
                if (row.Cells[j].Text == previousRow.Cells[j].Text)
                {
                    if (previousRow.Cells[j].RowSpan == 0)
                    {
                        if (row.Cells[j].RowSpan == 0)
                        {
                            previousRow.Cells[j].RowSpan += 2;
                        }
                        else
                        {
                            previousRow.Cells[j].RowSpan = row.Cells[j].RowSpan + 1;
                        }
                        row.Cells[j].Visible = false;
                    }
                }
            }
        }

        //Looping for TemplateField
        for (int i = GridViewApproval.Rows.Count - 1; i > 0; i--)
        {
            GridViewRow row = GridViewApproval.Rows[i];
            GridViewRow previousRow = GridViewApproval.Rows[i - 1];
            for (int j = 0; j < row.Cells.Count - 1; j++)
            {
                //Define what index on your template field cell that contain same value
                if (((LinkButton)row.Cells[9].FindControl("linkAction")).Text == ((LinkButton)previousRow.Cells[9].FindControl("linkAction")).Text)
                {
                    if (previousRow.Cells[9].RowSpan == 0)
                    {
                        if (row.Cells[9].RowSpan == 0)
                        {
                            previousRow.Cells[9].RowSpan += 2;
                        }
                        else
                        {
                            previousRow.Cells[9].RowSpan = row.Cells[9].RowSpan + 1;
                        }
                        row.Cells[9].Visible = false;
                    }
                }
            }
        }

        //This is to redefine again.
        for (int count = 0; count < GridViewApproval.Rows.Count; count++)
        {
            LinkButton lblprojectTest = GridViewApproval.Rows[count].Cells[9].FindControl("linkAction") as LinkButton;
            lblprojectTest.Text = "Details";
        }