RowNumber Column

14 posts, 3 answers
  1. ehsan
    ehsan avatar
    10 posts
    Member since:
    Sep 2010

    Posted 17 Oct 2010 Link to this post

    Hi
    I'm using 2010 Q2
    How can I add an unbound column to show row numbers?! Row numbers must be updated after sorting, filtering & grouping.
    thanks
  2. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 17 Oct 2010 Link to this post

    Hello ehsan,

    I would suggest something like this:
    To create the column and add it to the grid:
    var lineNumbersColumn = new GridViewDecimalColumn(typeof(int), "Line", "Line");
    lineNumbersColumn.AllowSort = false;
    //lineNumbersColumn.AllowFiltering = false;
    radGridView1.Columns.Add(lineNumbersColumn);

    And in the CellFormatting event, if the value for the cell is null, set the RowIndex + 1 if you don't want it to start with line 0, (why would you have to update line numbers?)
    void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        if (e.CellElement.ColumnInfo.Name == "Line" && e.CellElement.Value == null)
        {
            e.CellElement.Value = e.CellElement.RowIndex + 1;
        }
    }

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Answer
    devoas
    devoas avatar
    69 posts
    Member since:
    Dec 2009

    Posted 17 Oct 2010 Link to this post

    Dear Emanuel

    Since you are checking if value is null for line Number, means when Sort/Grouping/Filtering will be applied, Row number will not re-Updated. and  will be out of sequence.  I used same method but without checking for Null and every time cell value updated with RowIndex. This works perfectly but at the cost of performance in scrolling. This makes scrolling very slow.

    Please advise if there is any other way.

    Thanks
    devoas.


      
  5. ehsan
    ehsan avatar
    10 posts
    Member since:
    Sep 2010

    Posted 17 Oct 2010 Link to this post

    thank u EmanualI tried this approach, but the problem is that when u apply some filter on data,  only some rows will be shown while other are hidden. Rowindex for visible rows is still the original value ( It must be index among visible rows not all rows). did u get the  point?
  6. ehsan
    ehsan avatar
    10 posts
    Member since:
    Sep 2010

    Posted 17 Oct 2010 Link to this post

    Dear devoas
    Thanks to your advice, it work great for me
    god bless u ;-)
  7. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 17 Oct 2010 Link to this post

    Hello,

    I added that check for null for performance considerations, if just the scroll is the problem then you could set
    radGridView1.EnableFastScrolling = true;

    But i still think that there it should be a better solution.

    Best Regards,
    Emanuel Varga
  8. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 17 Oct 2010 Link to this post

    Please try the following:

    void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        if (e.CellElement.ColumnInfo.Name == "Line" && string.IsNullOrEmpty(e.CellElement.Text))
        {
            e.CellElement.Text = (e.CellElement.RowIndex + 1).ToString();
        }
    }

    I cannot get it to change the numbers, I've tried sort, filter, everything.

    Please let me know if this is OK in your case.

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
  9. devoas
    devoas avatar
    69 posts
    Member since:
    Dec 2009

    Posted 18 Oct 2010 Link to this post

    Dear Emanual

    Thanks for providing more details but I could not understand the different in your last code and previous one as both have same result. Kindly explain.  Actually it is required that  Row numbers should be changed after Sorting/Filtering/Grouping otherwise Numbers does not show proper values.  Like we have following Data and Row number updated in first instance.

    Line Value
    1. C
    2. D
    3. A

    After sorting on Value Column, if we do not remove the null or empty string checking the result will be following

    Line Value
    3. A
    2. C
    1. D

    So I asked if we should remove the null/empty string the results are perfect like following but scrolling gets very slow.. 

    Line
    Value
    1. A
    2. C
    3. D


    Even I tried    EnableFastScrolling = true;  
    but this makes scrolling to be happen after user complete Scrolling thru mouse button, and this does not seems good as scrolling is not done along with mouse movement..... 

    Please guide if you think there is any other better way to address this.

    Thanks,
    devoas
     


  10. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 18 Oct 2010 Link to this post

    Please try this application:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
     
    public partial class Form1 : Form
    {
        private RadGridView radGridView1;
        public Form1()
        {
            InitializeComponent();
            this.Controls.Add(radGridView1 = new RadGridView());
            radGridView1.EnableFiltering = true;
            radGridView1.Dock = DockStyle.Fill;
            radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        }
     
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            var lineNumbersColumn = new GridViewDecimalColumn(typeof(int), "Line", "Line");
            lineNumbersColumn.AllowSort = false;
            radGridView1.CellFormatting += new CellFormattingEventHandler(radGridView1_CellFormatting);
            radGridView1.Columns.Add(lineNumbersColumn);
     
            radGridView1.DataSource = new TestsCollection(100);
        }
     
        void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.CellElement.ColumnInfo.Name == "Line" && string.IsNullOrEmpty(e.CellElement.Text))
            {
                e.CellElement.Text = (e.CellElement.RowIndex + 1).ToString();
            }
        }
    }
     
    public class Test
    {
        public int Id
        {
            get;
            set;
        }
     
        public string Name
        {
            get;
            set;
        }
     
        public Test()
        {
        }
     
        public Test(int id, string name)
        {
            this.Id = id;
            this.Name = name;
        }
    }
     
    public class TestsCollection : BindingList<Test>
    {
        List<string> aToZ = Enumerable.Range('A', 26)
                              .Select(x => ((char)x).ToString())
                              .ToList();
     
        public TestsCollection(int noItems)
        {
            for (int i = 0; i < noItems; i++)
            {
                this.Add(new Test(i, aToZ[i % 26] + i.ToString()));
            }
        }
    }

    And tell me what I'm missing here., everything seems to be fine
    Best Regards,
    Emanuel Varga
  11. devoas
    devoas avatar
    69 posts
    Member since:
    Dec 2009

    Posted 18 Oct 2010 Link to this post

    Dear 

    Thanks for sending complete sample and it works perfectly as required. Now I got the  the difference between  e.CellElement.Value 
    and e.CellElement.Text.Using e.CellElement.Text Checking everything works perfectly. Thanks alot...

    Please confirm why this happen that Cellement.value consist the value after sorting/Grouping but CellElement.Text is empty.   

    Thanks
    devoas.
  12. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 18 Oct 2010 Link to this post

    Hello guys,

    I'm glad to hear that it's finally OK, i was beginning to think that i'm seeing things :).

    The difference between Text and Value is a simple one, and that is the Text property will always be calculated from the Value of the cell element, if, let's say we have a DateTime object with a FormatString, in the Value we will have a DateTime object, but the text property will reflect that formatted value, so here we if we want to avoid recalculating the Text always and cause those performance problems you were talking about we can just set the Text property.

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
  13. Lester
    Lester avatar
    4 posts
    Member since:
    Jan 2011

    Posted 01 Sep 2015 in reply to Emanuel Varga Link to this post

    Thanks, your solution worked perfectly !!
  14. Matthew
    Matthew avatar
    1 posts
    Member since:
    May 2015

    Posted 12 Nov 2015 in reply to Emanuel Varga Link to this post

    I tried the solution in VB.NET using the C# converter telerik provides.

    Private Sub dgDetail_RowFormatting(sender As Object, e As RowFormattingEventArgs) Handles dgDetail.RowFormatting
    If e.CellElement.ColumnInfo.Name = "Line" And String.IsNullOrEmpty(e.CellElement.Text) Then
    e.CellElement.Text = (e.CellElement.RowIndex + 1).ToString()
    End If
    End Sub

     

    however e.CellElement is coming up as not being a member of the RwFomattingEventArgs do I need to import a reference or something? I am already importing

    Imports System.Data.SqlClient
    Imports Telerik.WinControls
    Imports Telerik.Data
    Imports Telerik.WinControls.UI
    Imports System.ComponentModel

     

  15. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 13 Nov 2015 Link to this post

    Hi Matthew,

    In the example below, the CellFormatting is being used, while in your example, you are using the RowFormatting event, hence you do not see the e.CellElement property.

    I hope that you find this information useful.

    Regards,
    Stefan
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for WinForms is Visual Studio 2017 Ready