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

Hide Delete Button in Details Table based on condition

4 Answers 253 Views
Grid
This is a migrated thread and some comments may be shown as answers.
dbernett
Top achievements
Rank 2
dbernett asked on 08 Jun 2011, 05:51 PM
My last challenge to solve and this project will work.

I have a Delete button in a  DETAILS TABLE.   Using the GridButtonColumn with CommandName="Delete" and UniqueName="Delete Entry"

I need to be able to hide these based on certain conditions.  I would probably put the code in the ItemDataBound.  I would need to reference my second DataKeyName (QTRInserted) to decide to Hide or Show the Delete button.  I DO NOT WANT TO HIDE THE ENTIRE COLUMN.  Thanks.
<DetailTables>
             <telerik:GridTableView runat="server" DataSourceID="sqlDSDetails" Name="Details"
                 Width="100%" CommandItemDisplay="Top" DataKeyNames="Details_ID, QTRInserted">
                 <ParentTableRelation>

4 Answers, 1 is accepted

Sort by
0
dbernett
Top achievements
Rank 2
answered on 08 Jun 2011, 07:45 PM
I seem to have it working but it's sloppy.  I have to eat an exception.    Any betters ideas?

  Protected Sub grdMSC_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs)
   
        Dim QTRInserted As String = Nothing
          
        If e.Item.OwnerTableView.Name = "Details" AndAlso (TypeOf e.Item Is GridEditableItem) Then
              
            Try
                Dim item As GridEditableItem = DirectCast(e.Item, GridEditableItem)
                QTRInserted = item.GetDataKeyValue("QTRInserted")
            Catch
                ' each exception
            End Try
           
        End If
          
          
        If TypeOf e.Item Is GridDataItem And e.Item.OwnerTableView.Name = "Details" Then
              
            Dim item2 As GridDataItem = DirectCast(e.Item, GridDataItem)
            If QTRInserted <> CurrentQuarter Then
                item2("DeleteEntry").Visible = False
            End If
    
        End If
  
End If
0
Shinu
Top achievements
Rank 2
answered on 09 Jun 2011, 08:10 AM
Hello David,

You can access the Delete button from code and hide it like below.

ASPX:
<telerik:GridButtonColumn CommandName="Delete" Text="delete" UniqueName="DeleteEntry"
                        ButtonType="LinkButton">
 </telerik:GridButtonColumn>

VB.Net:
Protected Sub grdMSC_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridDataItem Then
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
        Dim QTRInserted As String = item.GetDataKeyValue("QTRInserted").ToString()
        If QTRInserted <> CurrentQuarter Then
            Dim btnDelete As LinkButton = DirectCast(item("DeleteEntry").Controls(0), LinkButton)
            btnDelete.Visible = False
        End If
    End If
End Sub

-Shinu.
0
dbernett
Top achievements
Rank 2
answered on 09 Jun 2011, 01:37 PM
Thanks but that code throws an error on the DataKeys retrieval.  This happens when I hit the Expand chevron.  I'm dealing with a Master Detail Hierachy Grid.

I did get it to work if I throw a Try Catch Block around it.


//To show Delete or NOT.  Only allow delete if same Quarter as entry
    if (e.Item is GridDataItem)
    {
        try
        {
            GridDataItem item = (GridDataItem)e.Item;
            string QTRInserted = item.GetDataKeyValue("QTRInserted").ToString();
            if (QTRInserted != CurrentQuarter)
            {
                LinkButton btnDelete = (LinkButton)item["DeleteEntry"].Controls[0];
                btnDelete.Visible = false;
            }
        }
        catch
        {// Eat Excepton  - fix later}
        }
    }



NO TRY CATCH Block produces this on Expand
Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
  
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error: 
Line 72: Dim QTRInserted As String = item.GetDataKeyValue("QTRInserted").ToString()
  
0
Shinu
Top achievements
Rank 2
answered on 13 Jun 2011, 04:54 AM
Hello David,

Have you set the DataKeyNames correctly?
aspx:
<MasterTableView DataKeyNames="QTRInserted" . . . . . . . .>

Thanks,
Shinu.
Tags
Grid
Asked by
dbernett
Top achievements
Rank 2
Answers by
dbernett
Top achievements
Rank 2
Shinu
Top achievements
Rank 2
Share this question
or