17 Answers, 1 is accepted
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
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.
Thanks to your advice, it work great for me
god bless u ;-)
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
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
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
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
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.
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
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
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
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
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
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
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.
Dess
Progress Telerik