This is a migrated thread and some comments may be shown as answers.

RowNumber Column

17 Answers 728 Views
GridView
This is a migrated thread and some comments may be shown as answers.
ehsan
Top achievements
Rank 1
ehsan asked on 17 Oct 2010, 07:50 PM
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

17 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 17 Oct 2010, 08:31 PM
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
0
Accepted
devoas
Top achievements
Rank 1
answered on 17 Oct 2010, 09:23 PM
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.


  
0
ehsan
Top achievements
Rank 1
answered on 17 Oct 2010, 09:31 PM
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?
0
ehsan
Top achievements
Rank 1
answered on 17 Oct 2010, 09:38 PM
Dear devoas
Thanks to your advice, it work great for me
god bless u ;-)
0
Emanuel Varga
Top achievements
Rank 1
answered on 17 Oct 2010, 09:41 PM
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
0
Emanuel Varga
Top achievements
Rank 1
answered on 17 Oct 2010, 09:46 PM
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
0
devoas
Top achievements
Rank 1
answered on 18 Oct 2010, 09:18 AM
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
 


0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 18 Oct 2010, 09:36 AM
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
0
devoas
Top achievements
Rank 1
answered on 18 Oct 2010, 11:00 AM
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.
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 18 Oct 2010, 12:34 PM
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
0
Lester
Top achievements
Rank 1
answered on 02 Sep 2015, 03:41 AM
Thanks, your solution worked perfectly !!
0
Matthew
Top achievements
Rank 1
answered on 12 Nov 2015, 07:42 PM

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

 

0
Stefan
Telerik team
answered on 13 Nov 2015, 06:44 AM
Hi Matthew,

In the example below, the CellFormatting is being used, in your example, you are using the RowFormatting event, hence you do not see .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
0
Abdul
Top achievements
Rank 1
answered on 29 Apr 2017, 06:24 AM

Hi Emanuel Varga ,

It will not update the Row Number when we are deleting/inserting rows.I need the Row numbers must be updated after deleting or inserting rows.

Thanks In Advance.

Regards

ABDUL HAFEEL

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 01 May 2017, 11:27 AM
Hello Abdul, 

Thank you for writing.  

It is necessary to trigger the CellFormatting event for all cells when a row is deleted for example. For this purpose, it is necessary to handle the RadGridView.UserDeletedRow event and refresh the grid:
private void radGridView1_UserDeletedRow(object sender, GridViewRowEventArgs e)
{
    this.radGridView1.MasterTemplate.Refresh();
}

The UserAddedRow event is fired when a new row is added to the grid.

I hope this information helps. Should you have further questions I would be glad to help.

Regards,
Dess
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
jamsheer
Top achievements
Rank 1
Veteran
answered on 29 Aug 2018, 05:26 AM

Hello
      The code I used to generate the Serial No is 

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();
    }
}

Its working fine, But when we take grid preview getting the blank column of Serial No

How can I overcome this issue
Thanks

Jamshi

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 29 Aug 2018, 10:44 AM
Hello, Jamsheer,           
 
To handle this case you have to use the PrintCellFormatting event and specify the Text for the desired cell:

private void radGridView1_PrintCellFormatting(object sender, PrintCellFormattingEventArgs e)
{
      if (e.Column.Name == "Line" && string.IsNullOrEmpty(e.PrintCell.Text))
    {
        e.PrintCell.Text = (e.Row.Index + 1).ToString();
    }
}



I hope this information helps. If you have any additional questions, please let me know.  
 
Regards,
Dess
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
ehsan
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
devoas
Top achievements
Rank 1
ehsan
Top achievements
Rank 1
Lester
Top achievements
Rank 1
Matthew
Top achievements
Rank 1
Stefan
Telerik team
Abdul
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
jamsheer
Top achievements
Rank 1
Veteran
Share this question
or