Click here to Skip to main content
15,920,603 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a gridview and this Code(down below) I want to Show the button if the Status is open, otherwise the button should be invisible.
But this Code, does not work. The button is always visible.


What I have tried:

<pre lang="c#"> protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (((GridView)sender).SelectedRow != null)
        {
            GridViewRow g = ((GridView)sender).SelectedRow;

            string assign= GridView1.SelectedRow.Cells[5].Text;

            btnassign.Visible = (assign == "open");
        }

    }






CSS
<asp:Button Text="assign" ID="btnassign"  Visible="false" OnClick="btnassign_Click" runat="server" />
Posted
Updated 7-Mar-17 10:26am
Comments
Karthik_Mahalingam 7-Mar-17 10:00am    
show the gridview mark up code
[no name] 7-Mar-17 10:00am    
And what does the debugger tell you about this code?
Richard Deeming 7-Mar-17 10:09am    
I'd start by tidying up that code slightly:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow row = ((GridView)sender).SelectedRow;
    string status = (row == null) ? null : row.Cells[5].Text;
    btnassign.Visible = (status == "open");
}

Then, debug your code and check that you've got a selected row, and the status value is what you're expecting.

You can do in different way of code.

1. Get the status value from the database as True/False as per your logic.Then inside item template of gridview add one button and in its visible properties you can directly set the status value by the help of "Eval"
Ex:

ASP.NET
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" 
                Width="486px" >
                <columns>
                    <asp:TemplateField HeaderText="Show Button">
                        <itemtemplate>
                            
                            <asp:Button runat="server" Visible='<%# Eval("Status") %>' Text="ButtonName" >





2. You can use the grid view gv_RowDataBound method.

Inside this grid view bind the status value in a hidden field by the help of item template.Then find the hidden field value from gv_RowDataBound method and as per the status show the button.

Ex:
C#
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            foreach (GridViewRow rowInfo in gv.Rows)
            {
                
              HiddenField  hfFProductId = (HiddenField)rowInfo.FindControl("hdfId");
              Button buttonID = (Button)rowInfo.FindControl("buttonID");
              if (hfFProductId=="Open") buttonID.Visible=true;
              else buttonID.Visible=false;
                
                            }
        }


Check this code it may help you.
Happy coding..
 
Share this answer
 
v3
Check inline comments

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int statusColumnIndex = 5;  // ensure the correct column index (zero based index )
            string assign = GridView1.SelectedRow.Cells[statusColumnIndex].Text;
            assign = assign.Trim().ToLower(); // trim for extra spaces, and lower the case for exact comparison 
            btnassign.Visible = (assign == "open");
        }


The above will work if you are using bound field column.
if you are using template column, for example a label control in it, then use
string assign = (GridView1.SelectedRow.FindControl("lblStatusID") as Label).Text;


First of all you should put a break point inside the event and explore the SelectedRow object and see actually what you are getting inside it, this is the best and effective way to solve any issue.
refer this article for debugging Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
 
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