Get Row Count from Repeater Asp.Net
<p>I'm trying to apply deletion of a column in a repeater for each item in it.  So far I've been able to apply that change in the Header Cell of that column, and also to the first row after the Header if  I put my code like this:</p>

<pre><code>   protected void rptBillHeaders_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        RepeaterItem item = e.Item;
        if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater rptLineItems = ((Repeater)item.FindControl("rptLineItems"));
            rptLineItems.DataSource = currentBill.LineItemsByXVersion[rowNumber].Sort(BillLineItem.SortColumn.LineItemNumber, System.Data.SqlClient.SortOrder.Ascending);
            rptLineItems.DataBind();

            rowNumber++;

            if (e.Item.Controls.Count &gt;= 5)
            {

                    var XDocColumn = (HtmlTableCell)e.Item.Controls[1];
                    if (lit.BillTypes.COD == "COD")
                    {
                        if (XDocColumn != null)
                        {
                            XDocColumn.Visible = false;
                        }
                    }

                 }
             }
        }
</code></pre>

<p>I assume that I need to get the Repeaters Count in order to iterate through the rows that the repeater is binding in order for me to achieve that solution. However, every time I run the application with a for loop I get:</p>

<pre><code> An exception of type 'System.InvalidCastException'
 occurred in F_GBA.dll but was not handled in user code

Additional information: Unable to 
cast object of type 'System.Web.UI.DataBoundLiteralControl'
to type 'System.Web.UI.HtmlControls.HtmlTableCell'.
</code></pre>

<p>Here's the code snippet:</p>

<pre><code> protected void rptBillHeaders_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        RepeaterItem item = e.Item;
        if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater rptLineItems = ((Repeater)item.FindControl("rptLineItems"));
            rptLineItems.DataSource = currentBill.LineItemsByXVersion[rowNumber].Sort(BillLineItem.SortColumn.LineItemNumber, System.Data.SqlClient.SortOrder.Ascending);
            rptLineItems.DataBind();

            if (e.Item.Controls.Count &gt;= 5)
            {                   
                for (int i = 1; i &lt;= item.Controls.Count; i++)
                {
                    var XDocColumn = (HtmlTableCell)e.Item.Controls[i];
                    if (lit.BillTypes.COD == "COD")
                    {
                        if (XDocColumn != null)
                        {
                            XDocColumn.Visible = false;
                        }
                    }
                }

            }

            rowNumber++;
        }
}
</code></pre>

<p>Mark - up code:</p>

<pre><code> &lt;asp:Repeater runat="server" ID="rptBillHeaders" OnItemDataBound="rptBillHeaders_ItemDataBound"&gt;
    &lt;HeaderTemplate&gt;
        &lt;table width="98%" align="center" class="grid" cellpadding="0" cellspacing="0" style="border:solid 1px #000000; border-top:none 0px #FFFFFF;"&gt;
                &lt;tr class="gridHeaderRow"&gt;
                    &lt;th&gt;&amp;nbsp&lt;/th&gt;
                    &lt;th&gt;&lt;asp:Label runat="server" ID="billVersionLabel" &gt;&lt;/asp:Label&gt;&lt;/th&gt; 
                    &lt;th&gt;Action Type&lt;/th&gt;
                    &lt;th id="XDocHeader" runat="server"&gt;FMMI Doc #&lt;/th&gt;
                    &lt;th&gt;X Status&lt;/th&gt;
                    &lt;th&gt;Total Amount&lt;/th&gt;
                    &lt;th&gt;Submitted By&lt;/th&gt;
                    &lt;th&gt;Date Submitted&lt;/th&gt;
                    &lt;th&gt;Date Processed&lt;/th&gt;
                &lt;/tr&gt;
    &lt;/HeaderTemplate&gt;
    &lt;ItemTemplate&gt;
        &lt;tr class="gridRow gridRowThickTopBorder"&gt;
            &lt;td style="padding: 5px;"&gt;
                    &lt;div id="divBillIcon" align="center" style="text-align:center;" class="icon ui-state-default ui-corner-all"&gt;
                        &lt;span class="ui-icon ui-icon-plus"&gt;&lt;/span&gt;
                    &lt;/div&gt;
            &lt;/td&gt;
            &lt;td class="Center"&gt;&lt;%# ((int)Container.DataItem) %&gt;&lt;/td&gt;
            &lt;td&gt;&lt;%# GetXActionType((int)Container.DataItem) %&gt; &lt;/td&gt;
            &lt;td id="XDocColumn" runat="server"&gt;&lt;%# GetXDocNumber((int)Container.DataItem) %&gt; &lt;/td&gt;
            &lt;%--&lt;td&gt;&lt;%# val.get(cache.XStatusDict, currentBill.ProcessedStatusesByXVersion[((int)Container.DataItem)].XStatus, "&amp;nbsp;") %&gt;&lt;/td&gt;--%&gt;
            &lt;td&gt;&lt;%# val.get(cache.BillStatusDict, currentBill.ProcessedStatusesByXVersion[((int)Container.DataItem)].XStatus, "&amp;nbsp;") %&gt;&lt;/td&gt;
            &lt;td class="Right"&gt;$&lt;%# currentBill.LineItemsByXVersion[((int)Container.DataItem)].GetTotalAmount().ToString("N2") %&gt;&lt;/td&gt;
            &lt;td&gt;&lt;%# currentBill.PendingStatusesByXVersion[((int)Container.DataItem)].CreateWebUserName %&gt;&lt;/td&gt;
            &lt;td&gt;&lt;%# convert.ToDateString(convert.ToDateTimeQ(currentBill.PendingStatusesByXVersion[((int)Container.DataItem)].CreateDate)) %&gt;&lt;/td&gt;
            &lt;td&gt;&lt;%# convert.ToDateString(convert.ToDateTimeQ(currentBill.ProcessedStatusesByXVersion[((int)Container.DataItem)].CreateDate)) %&gt;&lt;/td&gt;
        &lt;/tr&gt;
</code></pre>

<p>Any help will be greatly appreciated. </p>

<p>Thanks.</p>