Click here to Skip to main content
16,022,309 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am trying to hide consecutive data items in an objectlistview.

so.

data1 data2 data3
data1 data2 data4
data1 data2 data5
data1 data2 data6
data10 data3 data8

I would like to view it as. (ignnore lines)

data1 data2 data3
_____________data4
_____________data5
_____________data6
data10 data3 data8

What I have tried:

I have tried formatting the cell with a white foreground, but the mouse on hovering over the cell causes glitches.

C#
private string previousActivity = "";

private void ObjectListView_FormatCell(object sender, FormatCellEventArgs e)
{
    if (e.ColumnIndex == 1) 
    {
        var currentActivity = ((Children)e.Model).EventName;

        if (currentActivity != previousActivity)
        {
            e.SubItem.ForeColor = Color.Black;
        }
        else
        { 
            e.SubItem.ForeColor = Color.White;
        }
        
        previousActivity = currentActivity;
    }
}
Posted
Updated 10-Jun-24 22:03pm
v4
Comments
Richard Deeming 11-Jun-24 3:33am    
We can't help you fix code that we can't see.

Edit your question and post the relevant parts of your code. Explain precisely what you have tried and where you are stuck.

Without that, this is just a "write the code for me" demand.
Dave Kreskowiak 11-Jun-24 9:07am    
What's an "ObjectListView", because that's not in the standard .NET control library for any app type.

1 solution

A more robust approach to achieve your desired effect without glitches would involve custom drawing or grouping within the ObjectListView. Here's an approach:

private string previousActivity = "";

private void ObjectListView_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
    if (e.ColumnIndex == 1)
    {
        var currentActivity = ((Children)e.Item.RowObject).EventName;

        if (currentActivity != previousActivity)
        {
            // Draw the text normally
            e.DrawText();
        }
        else
        {
            // Fill the background with a custom color to hide consecutive items
            e.Graphics.FillRectangle(Brushes.White, e.Bounds);
            e.Graphics.DrawString(e.SubItem.Text, e.SubItem.Font, Brushes.White, e.Bounds);
        }

        previousActivity = currentActivity;
    }
    else
    {
        // Draw other columns normally
        e.DrawDefault = true;
    }
}
 
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