Solved

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

Posted on 2011-03-07
6
367 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 500 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

740 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