Solved

Only show hyperlink if results return specific value ASP.NET Gridview SQL c#

Posted on 2014-10-10
9
606 Views
Last Modified: 2014-10-20
I am working on some asp and having an issue trying to get my vision working.

So I currently have a sql table that has the following columns (xrefid,requested,status)

I have the datagrid populate based on xrefid and give me all values of the requested values.

However, I am looking to make it so that the "Requested" value if a hyperlink if status ='S' and not a hyperlink if Status='N'

So it would be similar to the picture attached.

GridView want
0
Comment
Question by:desiredforsome
  • 6
  • 3
9 Comments
 
LVL 28

Assisted Solution

by:sammySeltzer
sammySeltzer earned 500 total points
ID: 40374880
Try this:

<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
    runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Status" HeaderText="Status" ItemStyle-Width="150" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:HyperLink ID="lnkStatusYes" NavigateUrl='<%# Eval("Status","http://www.abc.com/web/sompage.aspx?id={0}") %>'
                    Text="Status" Visible='<%# Eval("Status").ToString() == "Y" %>'
                    runat="server" />
                <asp:Label ID="lblStatusNo" Text='<%# Eval("Status") %>' Visible='<%# Eval("Status").ToString() == "N" %>'
                    runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {            
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[1] { new DataColumn("Id", typeof(int)),
                        new DataColumn("Status",typeof(string)) });
        dt.Rows.Add(1, "Y");
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}

Open in new window


You may have to make a few changes.

Also, you will have to handle when data is empty or it is neither Y nor N.
0
 

Author Comment

by:desiredforsome
ID: 40379862
I have this error -

Error      12      An array initializer of length '1' is expected
0
 

Author Comment

by:desiredforsome
ID: 40379870
Also trying to see how this is linked to sql
0
 

Author Comment

by:desiredforsome
ID: 40379917
I am getting it to work somewhat. However It is genreating two columns. One that has the descrition and one tha thas the status.

I am trying to make the description the hyperlink if the status is "N" and the description should display fromt he sql command i wrote.

SELECT requested,status from outlookreport.dbo.requestdoc.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:desiredforsome
ID: 40379940
I think I got it. I must use the expression

Text='<%# Eval("Requested").ToString() %>'

This is what I got working from hyperlink. So now I just need to make it work for the label
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 40380050
So, what is not working as far as the label goes?
0
 

Assisted Solution

by:desiredforsome
desiredforsome earned 0 total points
ID: 40380295
Ok So I got it working and Sammy you are the bomb. I made some corretions to your code and got it to do exactly what I wanted.

<div style="overflow-y: scroll; width: 100%; height: 280px;">
                        <asp:GridView class="Grid" ID="GridView2" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
                            runat="server" AutoGenerateColumns="False" Width="381px">
                            <Columns>

                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:Image ID="imgCover" runat="server" AlternateText="Image Cover" Visible='<%# Eval("Status").ToString() == "N" %>' ImageUrl="images/checkbox1.png" />
                                        <asp:Image ID="Image1" runat="server" AlternateText="Image Cover" Visible='<%# Eval("Status").ToString() == "Y" %>' ImageUrl="images/nocheck1.png" />
                                        <asp:HyperLink ID="lnkStatusYes" NavigateUrl='<%# Eval("Status","requireddocs.aspx") %>'
                                            Text='<%# Eval("Requested").ToString() %>' Visible='<%# Eval("Status").ToString() == "Y" %>'
                                            runat="server" />
                                        <asp:Label ID="lblStatusNo" Text='<%# Eval("Requested").ToString() %>' Visible='<%# Eval("Status").ToString() == "N" %>'
                                            runat="server" />

                                    </ItemTemplate>
                                </asp:TemplateField>

                            </Columns>

                            <HeaderStyle BackColor="#3AC0F2" ForeColor="White"></HeaderStyle>
                        </asp:GridView>
                    </div>

Open in new window

 private void loadrequired()
    {
        SqlConnection sqlconnection1 = new SqlConnection("Data Source=192.168.2.121\\EMMSDE;Initial Catalog=outlookreport;User ID=jsmith;Password=s5993153492");
        SqlCommand cmd5 = sqlconnection1.CreateCommand();
        cmd5.CommandText="SELECT requested,status from outlookreport.dbo.requestdoc";
        
        
        sqlconnection1.Open();
        SqlDataReader reader2 = cmd5.ExecuteReader();
        GridView2.DataSource = reader2;
        GridView2.DataBind();
    }

Open in new window

0
 
LVL 28

Accepted Solution

by:
sammySeltzer earned 500 total points
ID: 40380515
well done!
0
 

Author Closing Comment

by:desiredforsome
ID: 40391506
Sammy had a great start and with a little combination of items I was able to produce the result needed.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 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

20 Experts available now in Live!

Get 1:1 Help Now