SharePoint - user gets acces denied error when selecting a record in SPGridView

Asked By Bookworm on 29-Mar-12 02:53 AM
Hi,

My problem is: user gets acces denied error when selecting a record in SPGridView if it does not have edit rights on page where is the web part ? How can it be fixed because i can not set edit rights to the users, on that page, and in the same time the user needs to select a record on the grid. 
any idea? because it is very urgent to me!

thnx
Jacob Söderblom replied to Bookworm on 29-Mar-12 03:08 AM
Try

SPSecurity.RunWithElevatedPrivileges(delegate()
{
   //your code here
});
Bookworm replied to Jacob Söderblom on 29-Mar-12 03:19 AM
it shows the same: access denied error when selecting the record of grid,
if i set the edit right for the page, then everything its ok, but the user MUST NOT have the edit rights to the page.
any other way to do this? why SPGrid is working with page privileges?

thnx
Somesh Yadav replied to Bookworm on 29-Mar-12 04:47 AM
thisWeb = thisSite.AllWebs;

This code requires Administrator previliges. Run it under Elevated Previleges: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx

Bookworm replied to Somesh Yadav on 29-Mar-12 04:51 AM
i tried this but no result :(

do you know why SPGrid View behaves like this? what is the relation with page provileges?
because any others buttons work perfictly.

if you have any idea pls write to me.

thnx
kalpana aparnathi replied to Bookworm on 29-Mar-12 06:10 AM
hi,

Try below code for making grid editable not edit  for users .


protected void SPGrid1_ItemDataBound(object sender, SPGridItemEventArgs e)
    {
    if ((e.Item is SPGridEditableItem) && (e.Item.IsInEditMode) && (!e.Item.OwnerTableView.IsItemInserted))
    {
      SPGridEditableItem eitem = (SPGridEditableItem)e.Item;
      eitem["columnUniqueName"].Enabled = false;
    }
    }

Regards