简体   繁体   中英

show last 4 digits in gridview

I have an asp.net gridview and a column that has digits eg 1234567, I want to show the last 4 digits like so xxx4567. Is this possible.

I am binding my gridview to a list of objects as below:

List<Details> objdet = Manager.Get_Details(ID);

             if (objdet!= null)
             {

                 gvDetails.DataSource = objdet;
                 gvDetails.DataBind();
             }

and in my grid i have this:

 <asp:GridView ID="gvDetails" runat="server" AllowPaging ="true" 
                AllowSorting ="true" AutoGenerateColumns="false" GridLines="None" CssClass="mGrid">
                <Columns>
                <asp:BoundField DataField="DetNumber" HeaderText="Number" ReadOnly="true" />
</Columns>
            </asp:GridView>

One way to do it is to add a property to your Details object like this:

public string Last4Digits
{
  //Return the last 4 digits
}

And then bind the grid to that property instead of your DetNumber.

get your value from the gridview and then apply this to your string:

var result = mystring.Substring(mystring.Length - Math.Min(4, mystring.Length));
   //this will show 4567 

if you want to add Xs before it go ahead and do:

result="XXX" + result;

It's useful to set up a helper method to handle formatting instead of trying to jam a bunch of code on the page front. So on the page front implement a Template Field so you can manually bind the field value and call your helper.

<asp:TemplateField HeaderText="My Field">
     <ItemTemplate>
         <asp:Label runat="server" ID="lblMyField" 
                Text='<%# CustomFormatter(Eval("MyField")) %>' />
     </ItemTemplate>
</asp:TemplateField>

Then for the custom method try something like this:

public string CustomFormatter(string input)
{
    if (input.Length < 5) return input;
     char[] characters = input.ToCharArray();
    for (int i = characters.Length - 5; i <= 0; i--)
    {
        characters[i] = 'X';
    }
   return new string(characters);
}
string input = "1234567";
var output = new string(input.Select((c, i) => i < input.Length - 4 ? 'x' : c)
                             .ToArray());

In your Details object, add another Property called something like NumberWithMask :

public const int NumberWithMaskLength = 4;

public string DetNumber { get; set; }

public string NumberWithMask
{
    get
    {
        if (!string.IsNullOrEmpty(DetNumber) && DetNumber.Length > NumberWithMaskLength)
        {
            return new string('x', DetNumber.Length - NumberWithMaskLength ) + DetNumber.Substring(DetNumber.Length - NumberWithMaskLength );
        }
        return string.Empty;
    }
} 

Then use the new field in the relevant BoundField :

<asp:BoundField DataField="NumberWithMask" HeaderText="Number" ReadOnly="true" /> 

I suggets you use RowDataBound event

void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {

      var value = e.Row.Cells[1].Text;
      e.Row.Cells[1].Text = value.Substring(value.Length - 5, value.Length - 1);
    }
  }

Add event

<asp:gridview onrowdatabound="GridView_RowDataBound"></asp:gridview>

I would add a property to your Details class like this:

public class Details
{
    public int Number { get; set; }

    public string MaskedNumber
    {
        get
        {
            var temp = Number.ToString();

            if (temp.Length <= 4)
            {
                return temp;
            }

            return new string('x', temp.Length - 4) + temp.Substring(temp.Length - 4);
        }
    }
}

Then just use that as a column in the GridView . You also then have that functionality to use wherever else you may be using the Details class.

Edit: made it not suck so much for numbers with a length <= 4.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM