Disabling command button

13 posts, 0 answers
  1. KawaUser
    KawaUser avatar
    57 posts
    Member since:
    Oct 2010

    Posted 26 Mar 2011 Link to this post

    I need to disable a command column button if a certain column has information in it.

    Thanks,
    Chuck
  2. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 26 Mar 2011 Link to this post

    Hello KawaUser.

    Just use the CellFormatting event to do that, the following example should give you a basic example on how to do this:

    void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        var commandCell = e.CellElement as GridCommandCellElement;
        if (commandCell != null)
        {
            if (someCondition)
            {
                commandCell.CommandButton.Enabled = false;
            }
            else
            {
                commandCell.CommandButton.Enabled = true;
            }
        }
    }

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga

    Telerik WinForms MVP
  3. UI for WinForms is Visual Studio 2017 Ready
  4. KawaUser
    KawaUser avatar
    57 posts
    Member since:
    Oct 2010

    Posted 26 Mar 2011 Link to this post

    Dim commandCell As GridCommandCellElement = e.CellElement
      
           If Not commandCell Is Nothing Then
      
               If e.Row.Cells("APPROVED_BY").Value = "" Then
      
                   commandCell.Enabled = True
     
             else
     
            commandCell.Enabled = False
      
               End If
      
           End If
    I put this together and it errors out. Any ideas?
  5. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 26 Mar 2011 Link to this post

    Errors out?

    What exception is it throwing?


    Best Regards,
    Emanuel Varga

    Telerik WinForms MVP
  6. KawaUser
    KawaUser avatar
    57 posts
    Member since:
    Oct 2010

    Posted 26 Mar 2011 Link to this post

    Exception has been thrown by the target of an invocation.
  7. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 26 Mar 2011 Link to this post

    Hello again,

    I just noticed that you are disabling the cell, please follow my example, just disable the Button.


    Best Regards,
    Emanuel Varga

    Telerik WinForms MVP
  8. KawaUser
    KawaUser avatar
    57 posts
    Member since:
    Oct 2010

    Posted 26 Mar 2011 Link to this post

    Dim commandCell As GridCommandCellElement = e.CellElement
     
          If Not commandCell Is Nothing Then
     
              If e.Row.Cells("APPROVED_BY").Value = "" Then
     
                  commandCell.CommandButton.Enabled = True
     
              End If
     
          EndIf
  9. KawaUser
    KawaUser avatar
    57 posts
    Member since:
    Oct 2010

    Posted 26 Mar 2011 Link to this post

    I am still getting the error with the above code.
  10. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 26 Mar 2011 Link to this post

    Hello again,

    Please check the name of the column and please first check for null Cell values.

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga

    Telerik WinForms MVP
  11. Svett
    Admin
    Svett avatar
    728 posts

    Posted 30 Mar 2011 Link to this post

    Hello Chuck,

    Could you share with us the code snippets from your application that show how you are adopting Emanuel's solution? This way, we will be able to give you an appropriate solution. Another option is to open a support ticket with an attached project where the issue occurs.

    Best wishes,
    Svett
    the Telerik team
  12. KawaUser
    KawaUser avatar
    57 posts
    Member since:
    Oct 2010

    Posted 30 Mar 2011 Link to this post

    I have created a command cell button as the first column in the gridview. I created this using the columns collection window. 

    I now need to disable buttons based on a value in the row, "Approved_By". If "Approved_By" is not = "" I need to disable the column. Meaning if this row has been approved disable the button respective to that row. 

    I have not made any advance since I posted this last. Please Help!
    Dim commandCell As GridCommandCellElement = e.CellElement
      
          If Not commandCell Is Nothing Then
      
              If e.Row.Cells("APPROVED_BY").Value = "" Then
      
                  commandCell.CommandButton.Enabled = True
      
              End If
      
          EndIf

    Thanks,

    Chuck
  13. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 31 Mar 2011 Link to this post

    Hello again,

    Could you please make the changes i've suggested in my previous post?

    Best Regards,
    Emanuel Varga

    Telerik WinForms MVP
  14. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 31 Mar 2011 Link to this post

    Ok, i've missed something in your last post, you've forgot the else statement,

    Try something like this:
    Private Sub RadGridView1_CellFormatting(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles RadGridView1.CellFormatting
        If (TypeOf (e.CellElement) Is GridCommandCellElement) Then
            Dim commandCell As GridCommandCellElement = e.CellElement
            If Not commandCell Is Nothing Then
                If e.Row.Cells("Name").Value = "" Then
                    commandCell.CommandButton.Enabled = True
                Else
                    commandCell.CommandButton.Enabled = False
                End If
            End If
        End If
    End Sub

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga

    Telerik WinForms MVP
Back to Top
UI for WinForms is Visual Studio 2017 Ready