I need to add a Mask/DisplayFormatString to view only last 4 digits in DevExpress GridViewDataColumn. As an example if the real account number is 123456789. Then it should display as *****6789.
Can you please help me with this.
<dx:GridViewDataColumn Caption="Bank Account Number" FieldName="BankAccountNumber"></dx:GridViewDataColumn>
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
As far as I know there is no such native display format feature in ASPxGridView which obscures a string partially for certain first characters (a password mask is available but obscures all characters), however you can handle ASPxGridView.CustomColumnDisplayText event to produce custom masking with this workaround:
protected void ASPxGridView1_CustomColumnDisplayText(object sender, DevExpress.Web.ASPxGridViewColumnDisplayTextEventArgs e)
{
// check column name first
if (e.Column.FieldName != "BankAccountNumber")
return;
// get column values for BankAccountNumber
string value = e.Value.ToString();
// set asterisk to hide first n - 4 digits
string asterisks = new string('*', value.Length - 4);
// pick last 4 digits for showing
string last = value.Substring(value.Length - 4, 4);
// combine both asterisk mask and last digits
string result = asterisks + last;
// display as column text
e.DisplayText = result;
}
Side Note: As the reference stated, the text provided via this event will be used when the ASPxGridView is printed or exported, probably you need separate ASPxGridView instance for export in multiple formats or printing.
Masking core example: Fiddle demo
Reference: ASPxGridView.CustomColumnDisplayText Event
Related issue:
ASPxGridView – How to use the CustomColumnDisplayText event handler
Method 2
Masking in for only input form, maybe you can use substring in serverside if you want view only 4 digit
Source :
https://demos.devexpress.com/aspxeditorsdemos/Features/MaskedInput.aspx
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0