Masking Account number to View only last 4 digits

2019-03-06 02:07发布

问题:

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>

回答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



回答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