ASP.Net GridView - Show the last 4 digits - c#

In my gridview, I have a column that I need everything masked but the last 4-digits.
This is my GridView:
<asp:GridView ID="GridView" runat="server" AutoGenerateColumns="False" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="br_id" DataSourceID="SqlDataSource6">
<Columns>
<asp:BoundField DataField="AccountNumber" HeaderText="Account Number" SortExpression="AccountNumber" />
</Columns>
</asp:GridView>
I tried the Right function of SQL Server in my SQLDataSource,
`SELECT '****-****-****' + RIGHT (AccountNumber, 4)` FROM....
the issue then is, when I hit edit, and save in my GridView, it will save this in the database, for example: ****-****-****-1234

Return 4 digits from the database. Only update the the account number in the database if you edit the account number.
If the users need to be able to see the full account number then it defeats the purpose of masking it in the first place.

Related

Delete row from aspnet gridView via column button

I want to delete a row from an aspnet table, that have columnButton with [delete] button.
This is my code:
<asp:GridView ID="GridInduccionPersona" runat="server" EnablePartialRendering="true" AutoGenerateColumns="False" DataKeyNames="RUT" CellPadding="4" ForeColor="#333333" GridLines="None" DeleteMethod="GridInduccionPersona_RowDeleting">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="RUT" HeaderText="Rut" InsertVisible="False"
.
.
.
**<asp:ButtonField ButtonType="Button" CommandName="Delete" HeaderText="Eliminar" ShowHeader="True" Text="Eliminar" />**
</Columns>
</asp:GridView>
but when I put the page on Chrome; said: The Select operation is not supported unless you specify SelectMethod.
I just want to acces to function in server, and delete from gridview and database.
I dont want to use linq or similar, Im working with stored procedure.
Any idea about that ? is SelectMethod important for delete from my column ? (I supposed that the button call the id, or some similar in gridview to delete)
From what I remember from ASP.net Webforms, the GridView needs the SelectMethod property configured to works properly.
SelectMethod should point to the method that obtains the records.
Without this property, GridView can't list the records, so you can't delete them.
So, set the SelectMethod property to target to the method that retrieves the records.

Asp.net/c# -> Gridview Pager doesn't get updated when gridview rows are filtered (or hidden)

I have the below code where I have some conditions because of which I have to hide the row from showing to end user. "ShowRow" is a boolean value that gets set in GetUnitOfMeasure function (not copied here) based on these conditions.
There are some real conditions which is forcing me to hide. I tried to include them while building data source but I have to hide it before display.
Problem I am facing is the paging is not getting refreshed based on total rows shown at the end. For example, if I have TOTAL of 200Rows in the grid and only 2 rows needs to be shown and if these 2 rows are found in paging 3, then when page is loaded it still shows paging 1 2 3 4 and 3rd page has the valid 2 rows.
Please suggest.I have also used "onDataBound" against gridview (not copied here) but I just hide some columns here.
ASPX page
<asp:GridView ID="SearchResults" runat="Server" AutoGenerateColumns="false" EnableViewState="false"
AllowPaging="true" PageSize="50" OnDataBound ="SearchResults_DataBound" OnRowDataBound="SearchResults_RowDataBound">
<RowStyle CssClass="EvenRow" />
<AlternatingRowStyle CssClass="OddRow" />
<Columns>
<asp:TemplateField meta:resourceKey="UmSellField">
<ItemStyle CssClass="alpha" />
<HeaderStyle CssClass="alpha" />
<ItemTemplate>
<asp:Label ID="UmSellLabel" runat="server" EnableViewState="false" Text='<%# GetUnitOfMeasure(Container.DataItem,false) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
codebehind
protected void SearchResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType.Equals(DataControlRowType.DataRow))
{
e.Row.Visible = showRow;
e.Row.Cells[0].Visible = showRow;
}
}

store image in database and retrieve in datagridview

I am trying out new things myself in .net.I want to store image url in database and retrieve it dynamically in gridview.I have tried using blob.But i do not want to store image in database but just the urls.Some solutions that i have found that we can use template field in gridview and somehow use bind function.Also I can store all images in project folder and retrieve the images from folder.On button click i want to display images in gridview.But still i am not able to proceed forward.This is my code so far....
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="Model_Id" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="Model_Id" HeaderText="Model_Id"
InsertVisible="False" ReadOnly="True" SortExpression="Model_Id" />
<asp:BoundField DataField="Model_Name" HeaderText="Model_Name"
SortExpression="Model_Name" />
<asp:BoundField DataField="Max_seats" HeaderText="Max_seats"
SortExpression="Max_seats" />
<asp:BoundField DataField="Image" HeaderText="Image" SortExpression="Image" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Model]"></asp:SqlDataSource>
</div>
inside your GridView add a template field i.e.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="Model_Id" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField HeaderText="Photo">
<ItemTemplate>
<img src='<%# Eval("Image") %>' />
</ItemTemplate>
</asp:TemplateField>
//other bound columns
</Columns>
</asp:GridView>
where Image is the column inside your table [Model], containing Image URL.
if Images don't show, check the relative url structure, you might need to prefix/adjust it like this:
/images/image1.jpg to ../images/image1.jpg (depends on relative location of your folder containing images)
it should work for you.
store the images in a folder in solution explorer of your project, if you want to save the link to the database then set the data type of the coloumn to varchar(MAX) and store the link of the image to database table like below.
1.If your folder name is Image, then store the url as ../Image/picture.jpg
2.go to quick task menu of the gridview and click edit fields and from the AVAILABLE FIELDS list choose "ImageField" and click add, the image field is then added to SELECTED FIELDS
3.move the "ImageField" in SELECTED FIELDS up or down to place it between other columns
4.Click the "ImageField" and ImageField properties will be displayed on the right side, GOTO
DATA and in "DataImageUrlField" select the Database table column name from where you want to retrieve the image .
Thats Should Work, ImageField will automatically generate the HTML code for your images to be displayed.

Sorting Specific Column in a GridView after a DataBound

I have given an access to stored procedure, which i'm not able to edit.
This Stored Procedure returns a Table with 2 Column, what I did is set a GridView's DataSource using SQLDataSource in this stored procedure. but I want this GridView to Sort an specific column to descending whenever this GridView Loads.
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource2" ForeColor="Black"
Width="58%" Height="125px" AllowPaging="True" AllowSorting="True"
PageSize="5" >
<Columns>
<asp:BoundField DataField="DateOccur" HeaderText="Login Date"
SortExpression="DateOccur" />
<asp:BoundField DataField="TotalMinutesPlayed" HeaderText="Total Minutes"
SortExpression="TotalMinutesPlayed" />
</Columns>
<AlternatingRowStyle BackColor="#EFEFEF" />
</asp:GridView>
Thanks!~
You could also use the Gridview.Sort method
See Here.
If you can't sort it by editing the SQL itself, you can sort it programmatically by binding a DataView object to the datasource, specifying a Sort expression for the DataView, and then binding the DataGrid to the DataView object.
For a walkthrough of some example code, see here.
I was using a slightly modified version of the example code I found on MSDN.
Gridview Sort Example on MSDN
By combining the example I found with the SortExpression property I can sort on any column even using proprietary sorts my specification called for.

Gridview Column Removing

I have a web application that I am working on(ASP.NET 2.0 C#). In it I have a GridView whose data source is an Oracle database. I get the data into the gridview in my codebehind, and don't set the datasource directly.
I wanted to create a hyperlink field (NAME) that takes me to a details page about a specific record. What ends up happening is that it creates the Hyperlink field as well as the regular field that it gets from the datasource, which I don't want. If I remove the field from my SELECT statement, it gives an error saying something like: "NAME" not found in datasource.
How can I eliminate the regular field, and get a hyperlink field instead? I have tried Gridview.Columns.Remove(columnlocation), but that won't work coz the columns don't exist there originally.
Please Help
Thank you.
Instead of removing columns, disable AutoGenerateColumns property of the gridview and set your hyperlink column manually like that :
<asp:gridview id="GridView1"
autogeneratecolumns="false"
runat="server">
<asp:HyperLinkField DataNavigateUrlFields="UserID"
DataNavigateUrlFormatString="UserDetails.aspx?id={0}"
DataTextField="UserName" />
</asp:gridview>
Sounds like you have AutoGenerateColumns property set to TRUE on your grid. This means that a column is generated for EVERY column you return from your query.
If you want to have some custom columns, you should set AutoGenerateColumns="false" and add all the columns to the GirdView as asp:BoundField and your Hyperlink column as asp:TemplateField
Let me know if I'm off the mark with this
here's some code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Name" />
<asp:BoundField DataField="Whatever" />
<asp:TemplateField>
<ItemTemplate>
<a href='<%# Eval("UserId", "URL_TO_USER?userId={0}") %>'>Details</a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Categories