This is a migrated thread and some comments may be shown as answers.

How to allow/disallow row Edit/Delete based on certain conditions..

2 Answers 206 Views
Grid
This is a migrated thread and some comments may be shown as answers.
gc_0620
Top achievements
Rank 1
gc_0620 asked on 04 Aug 2010, 02:08 AM

Hi all. I am using Visual Studio 08 SP1 with Telerik Asp.net ajax 2010 version..

Using below link as prototype, I would like to allow edit/delete functions for  certain rows  (Say allow edit/delete  to those Rows only if City = "London", if not edit/delete will be cancelled/not allowed).

http://demos.telerik.com/aspnet-ajax/grid/examples/programming/commanditem/defaultcs.aspx


Below is my radgrid and onitemcommand for cancel. 

Thanks

---------

<telerik:RadGrid ID="RadGrid1" AllowAutomaticUpdates="true" AllowAutomaticDeletes="true"
            DataSourceID="SessionDataSource1" AllowSorting="True" AutoGenerateColumns="true"
            AllowPaging="True" GridLines="None" runat="server" ShowFooter="True" AllowMultiRowSelection="True"
            PageSize="7" AllowMultiRowEdit="True" AllowAutomaticInserts="True" 
            OnItemDeleted="RadGrid1_ItemDeleted" onitemcommand="RadGrid1_ItemCommand">
            <PagerStyle Mode="NextPrevAndNumeric" />
            <MasterTableView Width="100%"   CommandItemDisplay="Top" DataSourceID="SessionDataSource1"
                DataKeyNames="CustomerID">
                <CommandItemTemplate>
                    <div style="padding: 5px 5px;">
                        Custom command item template    
                        <asp:LinkButton ID="btnEditSelected" runat="server" CommandName="EditSelected" Visible='<%# RadGrid1.EditIndexes.Count == 0 %>'><img style="border:0px;vertical-align:middle;" alt="" src="Images/Edit.gif" />Edit selected</asp:LinkButton>  
                         <asp:LinkButton ID="LinkButton2" runat="server" CommandName="InitInsert" Visible='<%# !RadGrid1.MasterTableView.IsItemInserted %>'><img style="border:0px;vertical-align:middle;" alt="" src="Images/AddRecord.gif" />Add new</asp:LinkButton>  
                        <asp:LinkButton ID="LinkButton3" runat="server" CommandName="PerformInsert" Visible='<%# RadGrid1.MasterTableView.IsItemInserted %>'><img style="border:0px;vertical-align:middle;" alt="" src="Images/Insert.gif" /> Add this Customer</asp:LinkButton>  
                        <asp:LinkButton ID="LinkButton1" OnClientClick="javascript:return confirm('Delete all selected customers?')"
                            runat="server" CommandName="DeleteSelected"><img style="border:0px;vertical-align:middle;" alt="" src="Images/Delete.gif" />Delete selected customers</asp:LinkButton>  
                        <asp:LinkButton ID="LinkButton4" runat="server" CommandName="RebindGrid"><img style="border:0px;vertical-align:middle;" alt="" src="Images/Refresh.gif" />Refresh customer list</asp:LinkButton>
                    </div>
                </CommandItemTemplate>
            </MasterTableView>
            <ClientSettings>
                <Selecting AllowRowSelect="True" EnableDragToSelectRows="True" />
            </ClientSettings>
        </telerik:RadGrid>


protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
       {
       // Here I need help to allow edit/delete for only Customers from London...
           if (e.CommandName == RadGrid.EditSelectedCommandName)
           {
                            
                 e.Canceled = true;
               }
           }

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 04 Aug 2010, 06:49 AM
Hello,

One option is you can try the following code snippet in ItemCommand event.

C#:
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
   {
     if (e.CommandName == RadGrid.EditSelectedCommandName)
       {
          GridDataItem item = (GridDataItem)RadGrid1.SelectedItems[0];
          if(item["City"].Text=="London")
              e.Canceled = true;
       }
   }

Another option is disabling the EditSelect button from client side when selecting the row itself. In OnRowSelected client event check for the cell value of column 'City' and  then disable the 'EditSelect' button in CommandItemTemplate accordingly.

Hope this information helps,
Princy.
0
gc_0620
Top achievements
Rank 1
answered on 04 Aug 2010, 11:35 PM
Thanks Princy; as always your solution works.

I appreciate your help. You helped me a lot...

All the best

Sincerely,

GC_0620
Tags
Grid
Asked by
gc_0620
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
gc_0620
Top achievements
Rank 1
Share this question
or