Search code examples
asp.netrowdatabound

RowDataBound how to print multiple labels


I am trying to calculate the date difference between two dates and want to show them in grid. I have created a table in the grid and the results can be printer there.

My RowDataBound is calculating everything but don't know how to print the results.

protected void grdADR_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblADRYear = (Label)e.Row.FindControl("lblADRYear");
            Label lblBRCIDADR = (Label)e.Row.FindControl("lblBRCIDADR");
            DataTable dt = new DataTable();

        string strQuery = "Select ADR_TYPE,DatePART(dayofyear,Document_Date) AS                       DayOfTheYearADR,Document_Date From ADR Where BRCID = '" + lblBRCIDADR.Text.ToString() + "'"
                        + " AND CAST(YEAR(Document_Date) AS VARCHAR(4)) = '" + lblADRYear.Text.ToString() + "' Order by DayOfTheYearADR";

        dt = objAccess.GetDataTable(strQuery);
        if (dt != null)
        {
            if (dt.Rows.Count > 0)
            {
                int DayMarker = 0;
                int LastDay = 0;

                List<Label> labels = new List<Label>();


                for (int rcnt = 0; rcnt < dt.Rows.Count; rcnt++)
                {

                    DataRow dr = dt.Rows[rcnt];
                    Label lblADRTimeLineStart = new Label();
                    lblADRTimeLineStart.ForeColor = System.Drawing.Color.Red;
                    int RowDay = int.Parse(dr["DayOfTheYearADR"].ToString());
                    if (LastDay != RowDay)
                    {
                        Label lblEmptyBlock = new Label();
                        lblADRTimeLineStart.ForeColor = System.Drawing.Color.Blue;
                        lblEmptyBlock.Width = (RowDay - DayMarker) * 3;
                        labels.Add(lblEmptyBlock);


                        Label lblADRBlock = new Label();
                        lblADRTimeLineStart.ForeColor = System.Drawing.Color.Green;
                        lblADRBlock.Width = 3;
                        labels.Add(lblADRBlock);
                        DayMarker = RowDay + 3;

                    }
                    LastDay = RowDay;
                }

            }
        }

    }

}

ASPX File

<asp:GridView ID="grdADR" runat="server" DataSource='<%# functADRTimeLine(Eval("Month").ToString()) %>'  AutoGenerateColumns="false" ShowHeader="true" AllowPaging="false" Width="1260px" GridLines="Both"
    BorderWidth="0" BorderColor="#839168" BorderStyle="Solid" CellPadding="0" BackColor="white" OnRowDataBound="grdADR_RowDataBound">



    <Columns>
        <asp:TemplateField Visible="false">
            <HeaderTemplate>BRCID</HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblBRCIDADR" runat="server" Text='<%# Bind("BRCID") %>'></asp:Label>
            </ItemTemplate>
            <ItemStyle Width="0px" Wrap="true" BorderColor="black" BorderStyle="Dotted" BorderWidth="1" HorizontalAlign="Center" />
            <HeaderStyle BorderColor="black" BorderStyle="Outset" BorderWidth="1" HorizontalAlign="Center" />
        </asp:TemplateField>
        <asp:TemplateField Visible="false">
            <HeaderTemplate>Year</HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblADRYEAR" runat="server" Text='<%# Bind("ADR_YEAR") %>'></asp:Label>
            </ItemTemplate>
            <ItemStyle Width="0px" Wrap="true" BorderColor="black" BorderStyle="Dotted" BorderWidth="1" HorizontalAlign="Center" />
            <HeaderStyle BorderColor="black" BorderStyle="Outset" BorderWidth="1" HorizontalAlign="Center" />
        </asp:TemplateField>
        <asp:TemplateField>

            <ItemTemplate>
                <table cellpadding="0" cellspacing="0" border="0">

                    <tr>
                        <td style="width: 150px">
                            <%--HERE I WANT TO PRINT MY RESULTS.--%> 
                        </td>

                        <td style="width: 1101px"></td>
                    </tr>
                </table>

            </ItemTemplate>

        </asp:TemplateField>



    </Columns>
    <HeaderStyle Height="25px" HorizontalAlign="Center" BackColor="#a8b194" />
    <RowStyle CssClass="grd_RowStyle" BorderColor="black" BorderStyle="Dotted" BorderWidth="1" Wrap="true" />
</asp:GridView>

Solution

  • There are many options for you to print the text One option would be

    Add a placeholder control in where you want the labels to be printed

    <asp:PlaceHolder ID="placeHolderLabels" runat="server"></asp:PlaceHolder>
    

    In the codebehind, find this placeholder like this

    PlaceHolder placeHolderLabels= (PlaceHolder)e.Row.FindControl("placeHolderLabels");
    

    Then add your labels to this placeholder control using the following code

    placeHolderLabels.Controls.Add(lblEmptyBlock); 
    placeHolderLabels.Controls.Add(lblADRBlock);
    

    Another option is, since you are printing table like structure, you can add a datalist/gridview/repeater control where you want to print the labels. Then you can bind your calculated list to this control, which is more cleaner