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
?

- 51,913
- 37
- 138
- 191

- 213
- 2
- 6
- 18
3 Answers
I find Muhammad Akhtar's answer is almost spot on except I need to change the if clause slightly in my case to cover all the conditions. My if clause is as follows.
if ((e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit ||
(e.Row.RowState & DataControlRowState.Alternate) == DataControlRowState.Alternate)
I didn't find any problem with the original one until I have a special value of e.Row.RowState
as "Alternate | Edit", which make
(e.Row.RowState == DataControlRowState.Edit ||
e.Row.RowState == DataControlRowState.Alternate) == false
Nevertheless, I should thank Muhammad Akhtar for pointing me towards the right direction.
Here is my complete code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ((e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit || (e.Row.RowState & DataControlRowState.Alternate) == DataControlRowState.Alternate)
{
TextBox txt = (TextBox)e.Row.FindControl("ControlID");
txt.ReadOnly = true;
}
}
PS: In order to make DropDownList readonly, you need to disable it in its OnDataBound event:
protected void DropDownList1_DataBound(object sender, EventArgs e)
{
((DropDownList)sender).Enabled = false;
}

- 4,203
- 7
- 56
- 93

- 101
- 3
- 5
There is no direct way to set the GridView
column to readonly. But You can set the controls to readonly that are in that column in the RowDataBound
event of your GridView
. 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;
}
}

- 3,343
- 6
- 23
- 39

- 51,913
- 37
- 138
- 191
-
When I try this I get Compiler Error Message: CS0123: No overload for 'grdProducts_RowDataBound' matches delegate 'System.EventHandler' – BillTetrault May 05 '11 at 13:09
-
replace this line with ---- protected void grdProducts_RowDataBound(object sender, GridViewRowEventArgs e) – Muhammad Akhtar May 05 '11 at 13:13
-
I think problem when you define event handle, just copy from my answer – Muhammad Akhtar May 05 '11 at 13:14
-
I think it may be more difficult, I left out I'm using BulEditGridView, event does fire but does not find control even if I change id to "ctl00_MasterBody_grdProducts_ctl02_txt0", which I do see in View Source – BillTetrault May 05 '11 at 13:30
-
can you try TextBox txt = (TextBox)e.Row.FindControl("txt0"); ? – Muhammad Akhtar May 05 '11 at 13:31
-
It does fin control and set readonly but still get edit box, txt.visible does make it not display, but need it readonly – BillTetrault May 05 '11 at 13:44
-
@BillTetrault Thank You! I wasn't given the readonly option, but `.Enabled = false;` works just as well for my purposes – user1985189 Apr 11 '13 at 19:56
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.

- 50,520
- 35
- 148
- 257