?
Solved

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

Posted on 2014-10-10
9
Medium Priority
?
716 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
[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
  • 6
  • 3
9 Comments
 
LVL 29

Assisted Solution

by:sammySeltzer
sammySeltzer earned 2000 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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
 

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 29

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 29

Accepted Solution

by:
sammySeltzer earned 2000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

752 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