Solved

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

Posted on 2011-03-07
6
365 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
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

829 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