Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
C#
I have a gridview with columns Action, Sigin Date and Signout date. The "Action Column" is a button field column. For every cell in the Signout date column, i want the button in the same row with the signout date to be enabled. If there is signout date, i want the button to be disabled. How do i do this?


What I have tried:

I have created and bound data to my gridview
Posted
Updated 10-Oct-16 5:40am

1 solution

Convert your ButtonField to TemplateField column so you can have a control over the controls within it:

ASP.NET
<asp:templatefield>


The at RowDataBound event, you could toggle the Enabled property of the Button based on your requirement. A quick example would be:

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
      if (e.Row.RowState != DataControlRowState.Edit){// check for RowState
              if (e.Row.RowType == DataControlRowType.DataRow){ //check for RowType
    
                     // Get the signout value
                     // Note: You may need to change the index of Cells based on the column order
                     string signoutDateString = e.Row.Cells[0].Text; 

                     if(string.IsNullOrEmpty(signoutDateString)){
                            //assuming your Action Column resides within the 5th column
                            //access the LinkButton from the TemplateField using FindControl method
                            LinkButton lb = (LinkButton)e.Row.Cells[4].FindControl("lnkSignOut"); 
                            if (lb != null){
                                 lb.Enabled = false;
                            }
                      }  
               } 
        }
}
 
Share this answer
 
v2

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