Solved

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

Posted on 2011-03-07
6
366 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

Industry Leaders: 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!

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

735 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