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

RadGrid context menu

1 Answer 60 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rahul
Top achievements
Rank 1
Rahul asked on 13 Feb 2014, 02:11 PM
Hi ,

Currently i am using a RadGrid in our application ,I am using context menu to display and hide columns at client side , Now the requirement is , suppose there are 5 columns in the grid by default  out of that using context menu i hided a column , so  there are 4 columns displaying , now when i will click on add new record of that grid view , in the edit mode i need only four text boxes instead of 5 ,it means the invisible column will take null or default value.


Please help.

1 Answer, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 18 Feb 2014, 11:28 AM
Hi Rahul,

In order to achieve the described behavior you could use the ItemDataBound event for RadGrid. In the handler you could check what are the hidden columns and hide them from the EditForm. The handler would look similar to the one below:

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
    {
        GridEditFormItem editFormItem = e.Item as GridEditFormItem;
 
        foreach (GridColumn column in RadGrid1.MasterTableView.Columns)
        {
            if (!column.Display)
            {
                // if RadComboBox is rendered in the edit form for the column the id will be 'RCB_' + column's unique name
                string textBoxID = "TB_" + column.UniqueName;
 
                Control txtBox = editFormItem.FindControl(textBoxID) as Control;
                txtBox.Parent.Parent.Visible = false;
            }
        }
    }
}


This illustrates how to hide a TextBox control from the EditForm. Similar approach could be used if RadComboBox or DropDownList is rendered in the EditForm. The ID for the RadComboBox will be 'RCB_' + column's unique name. For a DripDownList the prefix will be 'DDL_'.

The default value that will be inserted could be set with the DefaultInsertValue property for the column.

For convenience I am also attaching a sample project illustrating the described approach.

Regards,
Viktor Tachev
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the UI for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Rahul
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Share this question
or