Format decimal value in unbound columns
Hi,
Recently I tried to format the decimal values in the unbound columns, but I found a problem.
The decimal values should be kept the specified number of decimal digits which is set by the user. Let us take 2 as an example. To avoid dropping 0 (e.g., 23.50), the decimal values may need converting to string values. However, the number filter will fail. I was wondering if there is any workaround to format the decimal digits and at the same time keep the advanced number filter work properly.
Related code has been pasted here as follows.
// Set advanced filter type for unbound columns.
private static void DataGridStrikePriceVolumeTable_FilterItemsPopulating(object sender, GridFilterItemsPopulatingEventArgs e)
{
if (e.Column.MappingName == "StrikePrice" && e.Column.MappingName == "TotalVolume")
return;
e.FilterControl.AdvancedFilterType = AdvancedFilterType.NumberFilter;
e.FilterControl.SetColumnDataType(typeof(decimal?));
e.FilterControl.AscendingSortString = GridResourceWrapper.SortNumberAscending;
e.FilterControl.DescendingSortString = GridResourceWrapper.SortNumberDescending;
} // end method DataGridStrikePriceVolumeTable_FilterItemsPopulating
// Populate data of each day's volume for unbound columns.
private void DataGridStrikePriceVolumeTable_QueryUnboundColumnValue(object sender, GridUnboundColumnEventsArgs e)
{
if (e.UnBoundAction != UnBoundActions.QueryData
|| _strikePriceVolumeRowCollection == null
|| _strikePriceVolumeRowCollection[0].Length == 1)
return;
var strikePrice = Convert.ToDecimal(e.Record.GetType().GetProperty("StrikePrice")?.GetValue(e.Record));
// Round each volume value which is not null.
for (var nodeCount = 0; nodeCount < _nodeTotalCount; nodeCount++)
if (_strikePriceVolumeRowCollection[nodeCount][0] == strikePrice)
{
var dayVolume = _strikePriceVolumeRowCollection[nodeCount][2 + Convert.ToInt32(e.Column.MappingName)];
e.Value = dayVolume == null
? null
: (decimal?) decimal.Round((decimal) dayVolume / (Properties.Settings.Default.DayVolumeUnit * 100m), Properties.Settings.Default.DayVolumeDecimalDigits);
break;
} // end if
} // end method DataGridStrikePriceVolumeTable_QueryUnboundColumnValue
Regards,
Arvin
SIGN IN To post a reply.
8 Replies
1 reply marked as answer
VS
Vijayarasan Sivanandham
Syncfusion Team
October 5, 2020 06:55 PM UTC
Hi Arvin Staff,
Thank you for contacting Syncfusion support.
Based on provided information we suspect that in your application applying some format in GridUnboundColumn then applying a filter is like a String Type Filter behavior in SfDataGrid. In this case, you do not set the Format in GridUnboundColumn its working NumberFilter Filter behavior in SfDataGrid for the data type is Numeric. This is your requirement, or Could you please provide more details about your scenario with illustrations?
It will be helpful for us to check on it and provide you the solution at the earliest.
Thank you for contacting Syncfusion support.
Based on provided information we suspect that in your application applying some format in GridUnboundColumn then applying a filter is like a String Type Filter behavior in SfDataGrid. In this case, you do not set the Format in GridUnboundColumn its working NumberFilter Filter behavior in SfDataGrid for the data type is Numeric. This is your requirement, or Could you please provide more details about your scenario with illustrations?
It will be helpful for us to check on it and provide you the solution at the earliest.
Regards,
Vijayarasan S
Vijayarasan S
AS
Arvin Staff
October 6, 2020 01:37 AM UTC
Hi,
Attachment: 1_992446f1.zip
Please refer to the screenshot attached as an example. The left column is a GridNumericColumn and it is easy to change the number of decimal digits to be displayed after the decimal point by setting the NumberDecimalDigits property. The advanced filter type of it is NumberFilter. I want to achieve the same thing in the right column which should be a GridUnboundColumn. In reality, the number of this kind of columns depends on data. There may be one or more.
The following code block sets the advanced filter type of unbound columns.
private static void DataGridStrikePriceVolumeTable_FilterItemsPopulating(object sender, GridFilterItemsPopulatingEventArgs e)
{
if (e.Column.MappingName == "StrikePrice" && e.Column.MappingName == "TotalVolume")
return;
e.FilterControl.AdvancedFilterType = AdvancedFilterType.NumberFilter;
e.FilterControl.SetColumnDataType(typeof(decimal?));
e.FilterControl.AscendingSortString = GridResourceWrapper.SortNumberAscending;
e.FilterControl.DescendingSortString = GridResourceWrapper.SortNumberDescending;
} // end method DataGridStrikePriceVolumeTable_FilterItemsPopulating
Take the first line in the screenshot as an illustration. I want to keep 1 decimal digit in both columns. I format the values in the unbound columns by using the event QueryUnboundColumnValue (not the Format property) as said in my last post. One solution is to convert the decimal values to string ones to keep 1 decimal digit (without dropping 0), but the advanced filter type can definitely not NumberFilter, and this is not what I want. Another solution is to just use the function decimal.Round() to keep 1 decimal digit. This time, NumberFilter works, but 0 may be dropped. This is also not what I want. What I desire is to keep the specified number of decimal digits without dropping 0 and use NumberFilter but not StringFilter in unbound columns. I was wondering if there is any workaround.
Regards,
Arvin
Attachment: 1_992446f1.zip
VS
Vijayarasan Sivanandham
Syncfusion Team
October 6, 2020 03:14 PM UTC
Hi Arvin Staff,
Thanks for the udpate.
Currently, we are analyzing your requirement of “the specified number of decimal digits without dropping 0 and use NumberFilter but not StringFilter in unbound columns in SfDataGrid”. We will validate and update you the details on or before October 08, 2020.
Thanks for the udpate.
Currently, we are analyzing your requirement of “the specified number of decimal digits without dropping 0 and use NumberFilter but not StringFilter in unbound columns in SfDataGrid”. We will validate and update you the details on or before October 08, 2020.
We appreciate your patience until then.
Regards,
Vijayarasan S
VS
Vijayarasan Sivanandham
Syncfusion Team
October 9, 2020 06:19 PM UTC
Hi Arvin Staff,
Thank you for your patience.
Based on provided information your requirement can be achieved by using decimal.Round() method with decimal digit 1 without dropping 0 and SfDataGrid.FiltersItemPopulating event to get the NumberFilter in UnBoundColumn. Please refer the below code snippet,
Thank you for your patience.
Based on provided information your requirement can be achieved by using decimal.Round() method with decimal digit 1 without dropping 0 and SfDataGrid.FiltersItemPopulating event to get the NumberFilter in UnBoundColumn. Please refer the below code snippet,
|
this.sfDataGrid.QueryUnboundColumnValue += SfDataGrid_QueryUnboundColumnValue;
this.sfDataGrid.FilterItemsPopulating += SfDataGrid_FilterItemsPopulating;
private void SfDataGrid_FilterItemsPopulating(object sender, GridFilterItemsPopulatingEventArgs e) {
if (e.Column.MappingName == "UnboundColumn")
{
e.FilterControl.AdvancedFilterType = AdvancedFilterType.NumberFilter;
e.FilterControl.SetColumnDataType(typeof(decimal?));
e.FilterControl.AscendingSortString = GridResourceWrapper.SortNumberAscending;
e.FilterControl.DescendingSortString = GridResourceWrapper.SortNumberDescending;
}
}
private void SfDataGrid_QueryUnboundColumnValue(object sender, GridUnboundColumnEventsArgs e)
{
if (e.UnBoundAction != UnBoundActions.QueryData)
return;
decimal? strikePrice = Convert.ToDecimal(e.Record.GetType().GetProperty("CustomerID")?.GetValue(e.Record));
e.Value = decimal.Round((decimal)strikePrice,1);
} |
If this is not your requirement can you please share us below things?
1. Brief replication procedure/video illustration of your requirement
2. Please provide code snippet related customization in SfDataGrid.
3. If possible, please provide the issue reproduced sample
It will be helpful for us to check on it and provide you the solution at the earliest.
Regards,
Vijayarasan S
Regards,
Vijayarasan S
AS
Arvin Staff
October 11, 2020 08:59 AM UTC
Hi Vijayarasan,
The solution you have provided is what I have already achieved. The problem is when it comes to some integer values (e.g. decimal.Round(5000, 1)), the value displayed is not 5000.0 (which is what I prefer) but 5000.
Regards,
Arvin
VS
Vijayarasan Sivanandham
Syncfusion Team
October 12, 2020 03:49 PM UTC
Hi Arvin Staff,
Thanks for the update.
Based on provided information your requirement can be achieved by converting decimal value into “N1”stringformat then convert the string into decimal value in QueryUnBoundColumn event in SfDataGrid. Please refer the below code snippet,
Thanks for the update.
Based on provided information your requirement can be achieved by converting decimal value into “N1”stringformat then convert the string into decimal value in QueryUnBoundColumn event in SfDataGrid. Please refer the below code snippet,
|
private void SfDataGrid_QueryUnboundColumnValue(object sender, GridUnboundColumnEventsArgs e)
{
if (e.UnBoundAction != UnBoundActions.QueryData)
return;
//define the string format as N1
var strikePrice =Convert.ToDecimal((e.Record.GetType().GetProperty("CustomerID")?.GetValue(e.Record))).ToString("N1");
//then convert the string into decimal
e.Value = Convert.ToDecimal(strikePrice);
} |
Screenshot for your reference,
Sample Link: https://www.syncfusion.com/downloads/support/forum/158352/ze/ModifiedSample350858135
We hope this helps. Please let us know, if you require further assistance on this.
Regards,
Vijayarasan S
Marked as answer
AS
Arvin Staff
October 19, 2020 11:53 AM UTC
Hi,
Although I don't know how to explain it, your solution solves my problem perfectly. Thanks much!
Regards,
Arvin
VS
Vijayarasan Sivanandham
Syncfusion Team
October 20, 2020 06:42 AM UTC
Hi Arvin Staff,
Thanks for the update.
We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you😊.
Regards,
Vijayarasan S
SIGN IN To post a reply.
- 8 Replies
- 2 Participants
- Marked answer
-
AS Arvin Staff
- Oct 3, 2020 05:32 AM UTC
- Oct 20, 2020 06:42 AM UTC