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

Column formatting with leading and trailing blanks

3 Answers 45 Views
GridView
This is a migrated thread and some comments may be shown as answers.
pierre-jean
Top achievements
Rank 1
Veteran
Iron
pierre-jean asked on 14 Sep 2017, 05:32 PM

I have a column in a databound grid view that contains strings with leading and traiing blanks

in the exemple liste below I have replaced spaces by underscores:

__1_

__2_

_10_

__1a

__3_

I would like the sorting to take the blanks into account and, the sorted would look like:

__1_

__1a

__2_

__3_

_10_

but the spaces are ignored

any Idea ?

thanks in advance

 

3 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 15 Sep 2017, 09:39 AM
Hello Pierre,

I have tested this and the values are sorted like in your example (I have attached my test project). So how you expect the spaces to be taken into account?

Please note that the custom sorting functionality allows you to manually handle the sort operation and arrange the rows in any way you want: Custom Sorting | RadGridView.

I am looking forward to your reply.

Regards,
Dimitar
Progress Telerik
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
pierre-jean
Top achievements
Rank 1
Veteran
Iron
answered on 15 Sep 2017, 04:18 PM

Thanks for your answer

in the test programm the sort does indeed work as expected.

However in my programm it does not ...

The difference may come from the fact that the grid is databound to a table and in the table these values have no leading or trailing blanks. 

In order to try to fix the sorting I have added the follwoing formatting to the column values

                .MasterTemplate.Columns("noCompetition").FormatString = "{0,3:@@@}"

after the databound; the values displayed in the column do have leading blanks, but it seems that the sorting performs against the table value and not the displaed values ?

I have also tried to add the leading space in the cellformatting event:

            e.CellElement.Text = Space(4 - e.CellElement.Value.ToString.Length) & e.CellElement.Value.ToString

the displayed text has the leading spaces but the srting is still not what I need ...

Thanks for your advice on how to change this

 

0
Dimitar
Telerik team
answered on 18 Sep 2017, 07:38 AM
Hi Pierre,

Yes this is expected and the format is only applied to the displayed text, it is not applied to the value. Changing the text in the CellFormatting event will have the same results and will not affect the sorting functionality. You can use the custom sorting to compare the values after applying specific formatting. For example: 
public RadForm1()
{
    InitializeComponent();
    radGridView1.EnableCustomSorting = true;
    radGridView1.CustomSorting += RadGridView1_CustomSorting;
}
 
private void RadGridView1_CustomSorting(object sender, Telerik.WinControls.UI.GridViewCustomSortingEventArgs e)
{
    var row1 = (string)e.Row1.Cells["Test"].Value;
    var row2 = (string)e.Row2.Cells["Test"].Value;
 
    row1 = string.Format("{0,3:@@@}", row1);
    row2 = string.Format("{0,3:@@@}", row2);
 
    var result = string.Compare(row1, row2, StringComparison.InvariantCulture);
 
    e.SortResult = result;
}

Please let me know if there is something else I can help you with. 

Regards,
Dimitar
Progress Telerik
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.
Tags
GridView
Asked by
pierre-jean
Top achievements
Rank 1
Veteran
Iron
Answers by
Dimitar
Telerik team
pierre-jean
Top achievements
Rank 1
Veteran
Iron
Share this question
or