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

Disabling command button

12 Answers 460 Views
GridView
This is a migrated thread and some comments may be shown as answers.
KawaUser
Top achievements
Rank 2
KawaUser asked on 26 Mar 2011, 06:40 PM
I need to disable a command column button if a certain column has information in it.

Thanks,
Chuck

12 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 26 Mar 2011, 07:41 PM
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
0
KawaUser
Top achievements
Rank 2
answered on 26 Mar 2011, 08:00 PM
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?
0
Emanuel Varga
Top achievements
Rank 1
answered on 26 Mar 2011, 08:37 PM
Errors out?

What exception is it throwing?


Best Regards,
Emanuel Varga

Telerik WinForms MVP
0
KawaUser
Top achievements
Rank 2
answered on 26 Mar 2011, 08:39 PM
Exception has been thrown by the target of an invocation.
0
Emanuel Varga
Top achievements
Rank 1
answered on 26 Mar 2011, 08:40 PM
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
0
KawaUser
Top achievements
Rank 2
answered on 26 Mar 2011, 08:44 PM
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
0
KawaUser
Top achievements
Rank 2
answered on 26 Mar 2011, 08:44 PM
I am still getting the error with the above code.
0
Emanuel Varga
Top achievements
Rank 1
answered on 26 Mar 2011, 09:16 PM
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
0
Svett
Telerik team
answered on 30 Mar 2011, 04:00 PM
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
0
KawaUser
Top achievements
Rank 2
answered on 30 Mar 2011, 04:27 PM
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
0
Emanuel Varga
Top achievements
Rank 1
answered on 31 Mar 2011, 08:36 AM
Hello again,

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

Best Regards,
Emanuel Varga

Telerik WinForms MVP
0
Emanuel Varga
Top achievements
Rank 1
answered on 31 Mar 2011, 08:49 AM
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
Tags
GridView
Asked by
KawaUser
Top achievements
Rank 2
Answers by
Emanuel Varga
Top achievements
Rank 1
KawaUser
Top achievements
Rank 2
Svett
Telerik team
Share this question
or