Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to merge the common row in listview, but long time search i am unable to find out the solution, any one can please help.
Getting the Error:
Unable to cast object of type 'System.Web.UI.WebControls.Label' to type 'System.Web.UI.WebControls.TableCell'.


What I have tried:

C#
protected void List_Details_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
       

        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            ListViewDataItem dataItem = (ListViewDataItem)e.Item;
            int currentIndex = dataItem.DisplayIndex;

           
      
            string CurrentEmp = DataBinder.Eval(dataItem.DataItem, "EmpNumber").ToString(); 

            if (previousEmp != null && CurrentEmp == previousEmp)
            {
               

                //e.Item.FindControl("lbl_EmpNumber").Visible = false;
                //((HtmlTableCell)e.Item.FindControl("lbl_EmpNumber")).RowSpan = ((HtmlTableCell)List_Details.Items[dataItem.DisplayIndex - 1].FindControl("lbl_EmpNumber")).RowSpan + 1;

                e.Item.FindControl("lbl_EmpNumber").Visible = false;
                ((TableCell)e.Item.FindControl("lbl_EmpNumber")).RowSpan = ((TableCell)dataItem.FindControl("lbl_EmpNumber")).RowSpan + 1;

             
            
                //Label lblPrevious = (Label)List_Details.Items[dataItem.DisplayIndex].FindControl("lbl_EmpNumber");
                //lblPrevious.Attributes.Add("rowspan", (int.Parse(lblPrevious.Attributes["rowspan"]) + 1).ToString());
            }

            previousEmp = currentEmp;
Posted
Updated 5-Dec-23 20:54pm
v2
Comments
Maciej Los 6-Dec-23 2:55am    
You forgot to mention what's wrong with your code...
Anuragintit 6-Dec-23 3:56am    
Unable to cast object of type 'System.Web.UI.WebControls.Label' to type 'System.Web.UI.WebControls.TableCell'.
CHill60 6-Dec-23 3:14am    
This sort of thing should be done as part of populating the listview rather than row by row afterwards

Quote:
Unable to cast object of type 'System.Web.UI.WebControls.Label' to type 'System.Web.UI.WebControls.TableCell'.
Well there are two places where that error could occur, and they both refer to the same control:
C#
(TableCell)dataItem.FindControl("lbl_EmpNumber")
You haven't shown any of your markup, but the error is telling you that your lbl_EmpNumber control is a Label. You cannot cast a Label instance to a TableCell.

But even if you could, what you're doing wouldn't make any sense; you're trying to increase the row-span on a control which you have set to be invisible (ie: not rendered) on the previous line!
 
Share this answer
 
Comments
Maciej Los 6-Dec-23 7:02am    
5ed!
I'd suggest to use gridview control rather than a listview.

See:
How to Merge Cells with Equal Values in a GridView[^]
Rows and Columns Merging in ASP.NET GridView Control[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900