Click here to Skip to main content
15,922,696 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a column in SQL:

Status
open 
Close

and Gridview with Boundfield value='Status'

When a user selects a row and the Status == open then it should display a button. Otherwise ist hiden.


What I have tried:

C#
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {


        string y = Data.Rows[GridView1.SelectedIndex][5].ToString();
      

        if (y == "open")
        {
           btnAccept.Visible = true;
        }
        else
        {
            btnAccept.Visible = false;
        }
    }
Posted
Updated 1-Feb-17 4:22am
Comments
Richard MacCutchan 1-Feb-17 3:52am    
Have you used your debugger to see what value is in y at this point?
F-ES Sitecore 1-Feb-17 4:06am    
This is unlikely to work, you have to remember the GridView is a control used to display data, it isn't something used to store data. You are going to have white space, new lines, <br> elements and all sorts in your cells that you don't know about. Instead get the relevant data from the datasource you are binding the grid too as that will be the raw data you need.
Karthik_Mahalingam 1-Feb-17 4:08am    
is the button present in each row?

 //  I think you should hide show the button on gridview_rowdatabound event like this.

       //look this is just the pseudo code
        //put the button you want to show/hide in the template field of gridview and try to find the control in the below event [OnRowDataBound]
        protected void gridview1_rowdatabound(object sender, GridViewRowEventArgs e)
        {
            //find the button control
            Button btnStatus = (Button)Gridview1.FindControl("yourControlName");
            foreach (GridViewRow gvrow in Gridview1.Rows)
            {
                // now read the status column value and enable/disable the button
                string status = gvrow.Column[yourColumnIndexStartingFrom 0].Text;
                if (status == "Open")
                { btnStatus.Visible = true; }
                else
                { btnStatus.Visible = false; }

            }
        }
// Let me know if it helps or not
 
Share this answer
 
v4
As per your question you want to show or hide your button as per the Status of the grid.
So it can be done in GridView1_RowDataBound event.
Refer this code it may help you.

protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
string value = e.Row.Cells[Index].Text;// Here Index where your status shows
Button btn = (Button)GridView1.Columns[Index];// Here Index where your button exist
if (status == "Open")
{ btn.Visible = true; }
else
{ btn.Visible = false; }
}
}
 
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