How to set column as readonly on Gridview?

GridView template Column conditionally set to readonly

  • Is there a way to set a Template Column on a GridView to readonly from code behind. Like if test for Admin=true make readonly= false else readonly = true?

  • Answer:

    There is no direct way to set Gridview column to readonly. But You can set the controls to readonly that are in that column in RowDataBound event of your Gridivew. e.g. protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == DataControlRowState.Alternate) { TextBox txt = (TextBox)e.Row.FindControl("ControlID"); txt.ReadOnly = true; } }

BillTetrault at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

Yes, tap into ItemDataBound event, and for each row, either use a readonly control and an edit control and show/hide the right control for the job, or alternatively disable the edit control. There's no global readonly setting for templates. HTH.

Brian Mains

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.