Hiding command button column in new row?

15 posts, 1 answers
  1. Sunil
    Sunil avatar
    230 posts
    Member since:
    Jan 2004

    Posted 20 Oct 2010 Link to this post

    Is there a simple way to hide a command button column for a new row that displays at top of a grid? Command button 'Save' doesn't make any sense for a new row.
    Also, how would I hide the filter for a command button column?

    Thanks
    Sunil
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 20 Oct 2010 Link to this post

    Hi Sunil,

    Again this should be done in the CellFormatting event. You should be able to get to the button this way..
    this.radGridView1.CellFormatting += new CellFormattingEventHandler(radGridView1_CellFormatting);  
          
    void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)  
    {  
        if (e.CellElement.RowInfo is GridViewNewRowInfo &&  
            e.CellElement.ColumnInfo is GridViewCommandColumn)  
        {  
            ((RadButtonElement)e.CellElement.Children[0]).Visible = False;  
        }  
    }
    I'll look into how to remove the filter, but in the meantime, I hope that helps
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 20 Oct 2010 Link to this post

    Hello Sunil,

    For this you can use the CellFormatting event, and check for RowIndex == -1 and if the cell element is GridCommandCellElement, like so:
    void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        var newLineCommandCell = e.CellElement as GridCommandCellElement;
        if (newLineCommandCell != null)
        {
            if (e.CellElement.RowIndex == -1)
            {
                newLineCommandCell.CommandButton.Visibility = ElementVisibility.Collapsed;
            }
            else if (newLineCommandCell.CommandButton.Visibility == ElementVisibility.Collapsed)
            {
                newLineCommandCell.CommandButton.Visibility = ElementVisibility.Visible;
            }
        }
    }

    Please remember always when using the CellFormatting event to change some values / properties of the cell, you have to reset them, because of the virtualization mechanism that the grid uses.

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

    Best Regards,
    Emanuel Varga
  5. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 20 Oct 2010 Link to this post

    and this forum post should tell you how to hide the filtering for a specified column

    hope that helps
    richard
  6. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 20 Oct 2010 Link to this post

    Hello again,

    There is no filter available tor the command column, it is always disabled.

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

    Best Regards,
    Emanuel Varga
  7. Sunil
    Sunil avatar
    230 posts
    Member since:
    Jan 2004

    Posted 20 Oct 2010 Link to this post

    Hi Emanuel,
    Sorry I did not mention I am using Q1 2008 SP1. In that version, the filter for command button column shows, and when I double click on it, it gives me an error.

    Thanks
    Sunil

    System.ArgumentNullException
    Value cannot be null.  Parameter name: key
       at System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)     at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)     at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)     at Telerik.WinControls.UI.GridViewEditManager.GetDefaultEditor(IEditorProvider provider)     at Telerik.WinControls.UI.BaseGridBehavior.InitializeEditor(GridViewColumn column)     at Telerik.WinControls.UI.BaseGridBehavior.OnMouseDownLeft(MouseEventArgs e)     at Telerik.WinControls.UI.BaseGridBehavior.OnMouseDown(MouseEventArgs e)     at Telerik.WinControls.UI.RadGridView.OnMouseDown(MouseEventArgs e)     at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)     at System.Windows.Forms.Control.WndProc(Message& m)     at System.Windows.Forms.ScrollableControl.WndProc(Message& m)     at Telerik.WinControls.RadControl.WndProc(Message& m)     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)     at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  8. Sunil
    Sunil avatar
    230 posts
    Member since:
    Jan 2004

    Posted 20 Oct 2010 Link to this post

    Hi Richard,
    I am using Q1 2008 SP1 version.
    The code you suggested didn't work in cell formatting event, but when I used similar code in CurrentRowChanged event then it works.
    Only when I place the cursor in New Row are the buttons showing up, so I handled the CurrentRowChanged event with your code as below, and got the desired results.

    Thanks
    Sunil

    private void attributesRadGridView_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e)
            {
               if(_skipEvent)
                {
                    return;
                }
                 if (e.CurrentRow is GridViewNewRowInfo )
                 {
                     ((RadButtonElement)e.CurrentRow.Cells["Save"].CellElement.Children[0]).Visibility = ElementVisibility.Hidden;
                 }
    }
  9. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 20 Oct 2010 Link to this post

    Hello again,

    Try this:
    void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        if (e.CellElement.ColumnInfo.Name == "CommandColumn" && e.CellElement is GridFilterCellElement)
        {
            var filterCell = e.CellElement as GridFilterCellElement;
            filterCell.Visibility = ElementVisibility.Collapsed;
        }
    }

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

    Best Regards,
    Emanuel Varga
  10. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 20 Oct 2010 Link to this post

    Hi Sunil, 

    Glad you got that part working. Either the solution you have got from mine, or the one from Emanuel should work fine. 
    Richard
  11. Sunil
    Sunil avatar
    230 posts
    Member since:
    Jan 2004

    Posted 20 Oct 2010 Link to this post

    Hi Emanuel,

    In my version of radgridview (Q1 2008 SP1) , the filter cell is never hitting the cell formatting event. I tried but it did not work.

    Thanks
    Sunil
  12. Sunil
    Sunil avatar
    230 posts
    Member since:
    Jan 2004

    Posted 20 Oct 2010 Link to this post

    Hi Emanuel and Richard,

    I appreciate all your help.

    I came up with a very simple solution to hide a column filter. This can actually be used for any column, and not just command button column. Please look at code below that uses MasterGridViewInfo object, which I have always overlooked. I have 2 columns called 'Save' and 'Cancel' that are command button columns, and I never want to show filters for them. So I simply collapse them once the grid has been databound.

    Thanks
    Sunil

    private void attributesRadGridView_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
     {
         attributesRadGridView.MasterGridViewInfo.TableFilteringRow.Cells["Save"].CellElement.Visibility = ElementVisibility.Collapsed;
         attributesRadGridView.MasterGridViewInfo.TableFilteringRow.Cells["Cancel"].CellElement.Visibility = ElementVisibility.Collapsed;
     }



  13. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 20 Oct 2010 Link to this post

    Glad you got it sorted
    All the best
    Richard
  14. Answer
    Sunil
    Sunil avatar
    230 posts
    Member since:
    Jan 2004

    Posted 20 Oct 2010 Link to this post

    Actually,both hiding filters and command button columns can be done in the same manner , and all in the same event. This 'may' be be quicker in performance since its only a one time execution of code, and not a repeated execution.

    The complete solution to my initial question in this thread is in code below. Its important to disable the command button columns, otherwise even when the button is not visible, the command click event is still being fired when command button cell is clicked.

    Thanks
    Sunil

    private void attributesRadGridView_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
         {
             //hide command column filters for Save and Cancel command buttoncolumns
             attributesRadGridView.MasterGridViewInfo.TableFilteringRow.Cells["Save"].CellElement.Visibility = ElementVisibility.Collapsed;
             attributesRadGridView.MasterGridViewInfo.TableFilteringRow.Cells["Cancel"].CellElement.Visibility = ElementVisibility.Collapsed;
              
             //hide the buttons for Save and Cancel command button columns in New Row
             attributesRadGridView.MasterGridViewInfo.TableAddNewRow.Cells["Save"].CellElement.Children[0].Visibility = ElementVisibility.Collapsed;
             attributesRadGridView.MasterGridViewInfo.TableAddNewRow.Cells["Cancel"].CellElement.Children[0].Visibility = ElementVisibility.Collapsed;
             
             //disable the command click events for Save and Cancel command button columns in New Row
             attributesRadGridView.MasterGridViewInfo.TableAddNewRow.Cells["Save"].CellElement.Enabled = false;
             attributesRadGridView.MasterGridViewInfo.TableAddNewRow.Cells["Cancel"].CellElement.Enabled = false;
         }
  15. Mike
    Mike avatar
    1 posts
    Member since:
    Jul 2012

    Posted 08 Aug 2012 Link to this post

    I am trying to use the previous example in my code on databind but I keep getting an unrecognized argument "CellElement"
    private void radGridViewGeneral_DataBindingComplete(object sender, Telerik.WinControls.UI.GridViewBindingCompleteEventArgs e)
    {
         //hide command column filters for Save and Cancel command buttoncolumns
         radGridViewGeneral.MasterGridViewInfo.TableFilteringRow.Cells["Add"].CellElement.Visibility = ElementVisibility.Collapsed;
          
         //hide the buttons for Update and Delete command button columns in New Row
         radGridViewGeneral.MasterGridViewInfo.TableAddNewRow.Cells["Update"].CellElement.Children[0].Visibility = ElementVisibility.Collapsed;
         radGridViewGeneral.MasterGridViewInfo.TableAddNewRow.Cells["Delete"].CellElement.Children[0].Visibility = ElementVisibility.Collapsed;
         //disable the command click events for Save and Cancel command button columns in New Row
         radGridViewGeneral.MasterGridViewInfo.TableAddNewRow.Cells["Update"].CellElement.Enabled = false;
         radGridViewGeneral.MasterGridViewInfo.TableAddNewRow.Cells["Delete"].CellElement.Enabled = false;
     }

  16. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 09 Aug 2012 Link to this post

    Hi Mike,

    Thank you for writing.

    The approach previously provided concerns RadGridView prior its major upgrade in 2010. In the new version of the grid, such modifications should be applied in the ViewCellFormatting event: http://www.telerik.com/help/winforms/gridview-cells-formatting-cells.html.

    Here is a sample code:
    void radGridView1_ViewCellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
    {
        if (e.Row is GridViewNewRowInfo)
        {
            if (e.Column.Name == "Update" || e.Column.Name == "Delete")
            {
                ((GridCommandCellElement)e.CellElement).CommandButton.Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
            }
     
            if (e.Column.Name == "Save" || e.Column.Name == "Cancel")
            {
                ((GridCommandCellElement)e.CellElement).CommandButton.Enabled = false;
            }
        }
    }

    I hope this helps. Let us know if you have any other questions.
     
    Kind regards,
    Stefan
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
Back to Top
UI for WinForms is Visual Studio 2017 Ready