Search code examples
c#objectlistview

Using Renderer and FormatRow in ObjectListView


I've found an issue when using the ObjectListView Control: http://objectlistview.sourceforge.net/cs/index.html

I'd like to use the FormatRow to set custom font and color properties of each row. I'd also like to use a custom renderer to insert eg. user controls in one of the columns. However, the two seems to conflict.

The code:

//create some bogus list
List<MyObject> list = new List<MyObject>();
list.Add(new MyObject("Test11", "Test12", "Test13"));
list.Add(new MyObject("Test21", "Test22", "Test23"));
list.Add(new MyObject("Test31", "Test32", "Test33"));

//make the 2nd row red
listView1.FormatRow += delegate (object sender1, BrightIdeasSoftware.FormatRowEventArgs e1) {
    MyObject x = (MyObject)e1.Model;
    if (x.Col1 == "Test21")
    {
        e1.Item.BackColor = Color.Red;
    }
};

//add a custom renderer to col2. This will enable us to insert eg. user controls or do other crazy stuff. 
//when we add this, the FormatRow above wont't work. Even though it isn't in effect. Eg. return false.
olvColumn2.RendererDelegate = delegate (EventArgs e1, Graphics g, Rectangle r, object x)
{
    return false;
};

//set list
listView1.SetObjects(list);

I've also created a small sample project

Does anyone know a way around this?


Solution

  • I've found a solution, I think. Instead of using the RendererDelegate you should use the Renderer class. Eg.

    private class MyColumnRenderer : BrightIdeasSoftware.BaseRenderer
    {
          public override bool RenderSubItem(DrawListViewSubItemEventArgs e, Graphics g, Rectangle cellBounds, object x)
          {
              //do you own stuff here
    
              //default rendering
              return base.RenderSubItem(e, g, cellBounds, x);
          }
    }
    
    //...
    
    olvColumn2.Renderer = new MyColumnRenderer();