Reputation: 5600
I have an asp.net gridview and a column that has digits e.g. 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>
Upvotes: 0
Views: 2136
Reputation: 75316
string input = "1234567";
var output = new string(input.Select((c, i) => i < input.Length - 4 ? 'x' : c)
.ToArray());
Upvotes: 1
Reputation: 29000
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>
Upvotes: 0
Reputation: 18569
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" />
Upvotes: 0
Reputation: 9931
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.
Upvotes: 0
Reputation: 1350
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);
}
Upvotes: 1
Reputation: 1862
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;
Upvotes: 1
Reputation: 17964
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.
Upvotes: 1