Solved

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

Posted on 2011-03-07
6
357 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
  • 3
  • 2
6 Comments
 
LVL 13

Expert Comment

by:gamarrojgq
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
Ok, sorry, my bad, you have to add this to your codebehind as well


    public bool ButtonVisible(String invoicestatus) {
        return (invoicestatus == "UNPAID");  
    }
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Closing Comment

by:jonpwatson5
Comment Utility
Brilliant, thanks very much.

Really appreciate that :)
0
 
LVL 13

Expert Comment

by:gamarrojgq
Comment Utility
Excellent, glad to help
0
 
LVL 7

Expert Comment

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now