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

Hiding command button column in new row?

14 Answers 629 Views
GridView
This is a migrated thread and some comments may be shown as answers.
SUNIL
Top achievements
Rank 2
Iron
SUNIL asked on 20 Oct 2010, 07:00 AM
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

14 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 20 Oct 2010, 07:49 AM
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
0
Emanuel Varga
Top achievements
Rank 1
answered on 20 Oct 2010, 07:51 AM
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
0
Richard Slade
Top achievements
Rank 2
answered on 20 Oct 2010, 07:52 AM
and this forum post should tell you how to hide the filtering for a specified column

hope that helps
richard
0
Emanuel Varga
Top achievements
Rank 1
answered on 20 Oct 2010, 07:54 AM
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
0
SUNIL
Top achievements
Rank 2
Iron
answered on 20 Oct 2010, 08:09 AM
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)
0
SUNIL
Top achievements
Rank 2
Iron
answered on 20 Oct 2010, 08:20 AM
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;
             }
}
0
Emanuel Varga
Top achievements
Rank 1
answered on 20 Oct 2010, 08:22 AM
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
0
Richard Slade
Top achievements
Rank 2
answered on 20 Oct 2010, 09:06 AM
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
0
SUNIL
Top achievements
Rank 2
Iron
answered on 20 Oct 2010, 04:09 PM
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
0
SUNIL
Top achievements
Rank 2
Iron
answered on 20 Oct 2010, 04:53 PM
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;
 }



0
Richard Slade
Top achievements
Rank 2
answered on 20 Oct 2010, 04:56 PM
Glad you got it sorted
All the best
Richard
0
Accepted
SUNIL
Top achievements
Rank 2
Iron
answered on 20 Oct 2010, 05:39 PM
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;
     }
0
Mike
Top achievements
Rank 1
answered on 08 Aug 2012, 02:03 PM
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;
 }

0
Stefan
Telerik team
answered on 09 Aug 2012, 10:33 AM
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 >>
Tags
GridView
Asked by
SUNIL
Top achievements
Rank 2
Iron
Answers by
Richard Slade
Top achievements
Rank 2
Emanuel Varga
Top achievements
Rank 1
SUNIL
Top achievements
Rank 2
Iron
Mike
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or