Solved

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

Posted on 2014-10-10
9
632 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

15 Experts available now in Live!

Get 1:1 Help Now