Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# - Show Content In Datagrid Cell Depending on Database Value

Posted on 2011-03-07
6
Medium Priority
?
375 Views
Last Modified: 2012-05-11
Hi.

have been looking at some solutions on here but wasn't able to work out how to do what i need.

I have an online area where customers can view their invoices i post for them. The details are put into a database and the datagrid pulls it through to display it to the customer.

I have a field called InvoiceStatus.

If InvoiceStatus is showing UNPAID then i want an extra column with a button that says 'PAY NOW'. When the button is pressed the user is sent to pay.aspx?number={0}

If the InvoiceStatus is showing PAID then i dont want the button to appear.

This will be for an online C# .net website.

Can anyone help?

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlSelectInvoices" AllowSorting="True" CellPadding="4" 
        ForeColor="#333333" GridLines="None" 
        onselectedindexchanged="GridView1_SelectedIndexChanged">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:BoundField DataField="InvoiceNumber" HeaderText="Invoice Number" 
                SortExpression="InvoiceNumber" />
            <asp:BoundField DataField="InvoiceAmount" HeaderText="Total Amount" 
                SortExpression="InvoiceAmount" />
            <asp:BoundField DataField="InvoiceIssueDate" HeaderText="Issue Date" 
                SortExpression="InvoiceIssueDate" />
            <asp:BoundField DataField="InvoiceDueDate" HeaderText="Payment Due Date" 
                SortExpression="InvoiceDueDate" />
            <asp:BoundField DataField="InvoiceStatus" HeaderText="Status" 
                SortExpression="InvoiceStatus" />
            <asp:HyperLinkField DataNavigateUrlFields="InvoiceNumber" 
                DataNavigateUrlFormatString="invoiceinfo.asx?number={0}" 
                NavigateUrl="invoiceinfo.aspx" ShowHeader="False" Text="Invoice Details" />
        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlSelectInvoices" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        SelectCommand="SELECT * FROM [tblInvoices] WHERE ([UserID] = @UserID)">
    </asp:SqlDataSource>

Open in new window

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;

public partial class OnlineAccounts_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["CurrentUser"] = User.Identity.Name;
    }

}

Open in new window

0
Comment
Question by:jonpwatson5
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35062781
Hi,

First you have to add a template column to your grid like this


                <asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
                            CommandName="GoAndPaid" Text='PAY'
                            CommandArgument='<%# Eval("InvoiceNumber") %>'
                            Visible = '<%# ButtonVisible(Eval("InvoiceStatus").ToString()) %>' onclick="LinkButton1_Click"
                            ></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>



And in your codebehind add this



    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        LinkButton paidButton = (LinkButton)sender;
        Response.Redirect("pay.aspx?number=" + paidButton.CommandArgument);
    }

Hope that helps

0
 

Author Comment

by:jonpwatson5
ID: 35062890
Error      1      The name 'ButtonVisible' does not exist in the current context      Invoices.aspx      31      


<Columns>
            <asp:BoundField DataField="InvoiceNumber" HeaderText="Invoice Number"
                SortExpression="InvoiceNumber" />
            <asp:BoundField DataField="InvoiceAmount" HeaderText="Total Amount"
                SortExpression="InvoiceAmount" DataFormatString="{0:c}" />
            <asp:BoundField DataField="InvoiceIssueDate" HeaderText="Issue Date"
                SortExpression="InvoiceIssueDate" />
            <asp:BoundField DataField="InvoiceDueDate" HeaderText="Payment Due Date"
                SortExpression="InvoiceDueDate" />
            <asp:BoundField DataField="InvoiceStatus" HeaderText="Status"
                SortExpression="InvoiceStatus" />
            <asp:HyperLinkField DataNavigateUrlFields="AccountNumber,InvoiceNumber" ShowHeader="False"
                Text="Invoice Details" InsertVisible="False"
                DataNavigateUrlFormatString="pdf/AC{0}IN{1}.pdf" Target="_blank" />
        <asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
                            CommandName="GoAndPaid" Text='PAY'
                            CommandArgument='<%# Eval("InvoiceNumber") %>'
                            Visible = '<%# ButtonVisible(Eval("InvoiceStatus").ToString()) %>' onclick="LinkButton1_Click"
                            ></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                </Columns>
0
 
LVL 13

Accepted Solution

by:
gamarrojgq earned 2000 total points
ID: 35063080
Ok, sorry, my bad, you have to add this to your codebehind as well


    public bool ButtonVisible(String invoicestatus) {
        return (invoicestatus == "UNPAID");  
    }
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Closing Comment

by:jonpwatson5
ID: 35063119
Brilliant, thanks very much.

Really appreciate that :)
0
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35063189
Excellent, glad to help
0
 
LVL 7

Expert Comment

by:nbove
ID: 35070476
<asp:HyperLinkField Visible='<%# Eval("InvoiceStatus").ToString() == "UNPAID" %>' DataNavigateUrlFormatString="pay.aspx?number={0}" DataNavigateUrlFields="InvoiceNumber" Text="PAY NOW" ShowHeader="false" />
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question