Menu

Make the first column display line number?

JflyCN
2010-12-30
2014-07-15
  • JflyCN

    JflyCN - 2010-12-30

    How to make the first column display line number?
    I guess it should have the following features:
    1. it should not be included in the data model
    2. it automatically maintains 1.2.3... values when the rows are reordered

    Any suggestions?

    Thanks.

     
  • Phillip Piper

    Phillip Piper - 2011-01-02

    Listen for the FormatRow event and set the text of the subitem that you want
    to contain line number.

    private void olvSimple_FormatRow(object sender, FormatRowEventArgs e)
    {
        e.Item.SubItems[1].Text = e.RowIndex.ToString();
    }
    
     
  • Phillip Piper

    Phillip Piper - 2011-01-02

    Sorry the above code should have used DisplayIndex, not RowIndex. DisplayIndex
    will always give the correct line number, even when the control is grouped.

    private void olvSimple_FormatRow(object sender, FormatRowEventArgs e)
    {
        e.Item.SubItems[1].Text = e.DisplayIndex.ToString();
    }
    
     
  • onclethomas

    onclethomas - 2014-02-06

    Hi, sorry to be a gravedigger.

    I found this thread searching for the problem that for some OLVs the first column does not get filled with the number. I have several OLVs with the same settings in my project and for only a few it is not working.

    The event fires and right after the instruction, the Text of the SubItem is correct. But some time after it get cleared again. Have you any idea why this is caused?

    Thanks

     
  • Phillip Piper

    Phillip Piper - 2014-02-06

    That code should always work. The FormatCell should be triggered every time a ListViewItem is built/rebuilt, and there should be no other way to change the contents of the ListViewItem.

    It is always possible that there is some bug in my code that makes it not work correctly :)

    Can you narrow down the steps that occur to make the values disappear?

    Regards,
    Phillip

     
  • Klaus

    Klaus - 2014-07-15

    I have the exact problem. To narrow it down I'll say I only have the problem on the first column. When SubItem index is set to any column number, its OK and row number will be shown. However on the first subitem this does not work. Here is my code:


    protected override void OnFormatRow(FormatRowEventArgs args)
    {
    base.OnFormatRow(args);
    if (mAutoIndexRow)
    args.Item.SubItems[0].Text = args.DisplayIndex.ToString(); // This does not work
    args.Item.SubItems[1].Text = args.DisplayIndex.ToString(); // This works
    }

    PS: I have tested the code on an event handler too. No difference between a handler and overriden method.

     

    Last edit: Klaus 2014-07-15
  • Klaus

    Klaus - 2014-07-15

    This 2nd post was not correct. I decided to delete it but I could not find the delete button! The above post is the only correct one.

     

    Last edit: Klaus 2014-07-15

Log in to post a comment.