Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my Website, I currently use a grid view which generates data from 3 tables namely status, Project_glance, Application_header. SQL Query returns 4 columns but in my grid-view i only shows 3 columns. The last column returns the Status_id of the project.The following is my .aspx code:

ASP.NET
<asp:GridView ID="grdProf" runat="server" AllowPaging="True" AutoGenerateColumns="false" OnPageIndexChanging="grdProf_PageIndexChanging">
 <Columns>
    <asp:TemplateField>
        <ItemTemplate>
           <asp:HyperLink ID="hlnkView" Visible="true" Text="View" runat="server" >   </asp:HyperLink>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="ApplicationID" HeaderText="ApplicantionID" />
    <asp:BoundField DataField="PRGLProjectTitle" HeaderText="Project Title" />
    <asp:BoundField DataField="Status" HeaderText="Project Status" />
 </Columns>
</asp:GridView>


If the Status_id > 15 then only view hyperlink will visible otherwise the View hyperlink text will be 'Edit' and a navigate URL will add to this hyperlink and another hyperlink 'Delete' will show to allow user to delete the project.

Please help me to find the correct solution for this..
Posted
Updated 27-Dec-11 19:45pm
v2
Comments
Sunasara Imdadhusen 28-Dec-11 1:46am    
added <PRE> tag for readability.

Hi you can use following code to show hide Hyperlink in gridview according to your condition.

add DataKeyNames to your GridView

XML
<asp:GridView DataKeyNames="Status_id" ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:HyperLink ID="hlnkView" Visible="true" Text="View" runat="server">   </asp:HyperLink>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>


add following code to gridview RowDataBound event

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            int StatusID = Convert.ToInt16(GridView1.DataKeys[e.Row.RowIndex].Values["Status_id"].ToString());
            e.Row.FindControl("hlnkView").Visible = (StatusID > 15) ? false : true;
        }



Thanks,
Imdadhusen
 
Share this answer
 
Comments
Sara Dark 28-Dec-11 2:00am    
i liked your solution more :)
this might not be the best solution but in case you didn't find any other you can use this one use 2 gridviews and in the datasource in the selectcommand in one of them put the condion status_id>15 in the other put status_id<=15
 
Share this answer
 
Comments
Sunasara Imdadhusen 28-Dec-11 7:19am    
Thanks you very much :)
you try like this..

change page design like this..

ASP.NET
<ItemTemplate>
     <asp:HyperLink ID="hlnkView" Visible="true" Text="View" runat="server" ></asp:HyperLink>
     <br />
        <asp:HyperLink ID="hlnkDelete" Visible="false" Text="Delete" runat="server"></asp:HyperLink>
 </ItemTemplate>


C#
//On GridView DataBound Event add the below code..

GridViewRow grdrow = e.Row;
HyperLink lnk = (HyperLink)grdrow.FindControl("hlnkView");
int statusID = Convert.ToInt32(grdrow[grdrow.RowIndex][3].ToString();
if !(statusID > 15)
{
    lnk.Text="Edit";
    lnk.NavigateUrl = "Your Edit URL";
    HyperLink lnk = (HyperLink)grdrow.FindControl("hlnkDelete");
    lnk.Visible=True;
}


hope it works..
 
Share this answer
 
v2
C#
you can use iif(coniditionhere, result if true, result if false)
<asp:hyperlink id="hlnkView" visible="<%#iif(Eval("StatusID")>15,"true","false")%>" text="View" xmlns:asp="#unknown"> </asp:hyperlink>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900