Datagrid - Make Edit or Read Only?

lorena

Centurion
Joined
Oct 23, 2003
Messages
134
Location
Phoenix, Arizona
I have a datagrid that allows editing. If a certain group of users accesses the page, however, I would like the page set up to only edit one field and not all of the edit fields? The admin user would have the ability to edit all fields in the grid.
Can this be done through ItemDataBound? If so, does anyone have an example of how to do it?
Hope this makes sense.
 
Code:
Int32 myEditIndex = -1;

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
  myEditIndex = e.NewEditIndex;
}

protected void GridView1_PreRender(object sender, EventArgs e)
{
  if (myEditIndex > -1)
  {
	GridView1.Rows[myEditIndex].Cells[1].Enabled = false;
  }
}

What you want to do, has to be done in the PreRender event of the grid. I did'nt find a way to affect the ReadOnly property directly, but this one works.
And don't forget to reset the the var myEditIndex to -1 in RowEditCanceling and RowUpdating!
 
Welcome, but I fear you won't be so happy with my solution, because I just realized that you were looking for the datagrid. And this sounds much like asp.net 1.1 and I gave you a solution for the gridview of version 2.0.

In version 1.1 you can try something that raises a conversionexception in 2.0 during compiletime (strange but true). So here you should be able to change the ReadOnly property directly:

Code:
BoundColumn col = (BoundColumn)DataGrid1.Column(1);
col.ReadOnly = false;
 
Back
Top