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

[Solved] Grouping, Exporting, and Refreshing not working

1 Answer 80 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Matt
Top achievements
Rank 1
Matt asked on 22 Apr 2013, 03:41 PM
I have the following code.  When I try to group, refresh ("Restore Defaults"), or Export, nothing happens.  How do I make these events work?

ASPX Code
<telerik:RadGrid ID="RadGrid1" runat="server">
</Telerik:RadGrid>

ASPX.CS Code
protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
 
    if (!IsPostBack)
    {
        CreateDefaultTelerikGrid();
    }
}
 
List<string> timeframes = new List<string>();
 
protected void WireRadGridEvents()
{
    RadGrid1.NeedDataSource += this.RadGrid1_NeedDataSource;
    RadGrid1.ItemCreated += this.RadGrid1_ItemCreated;
    RadGrid1.ItemCommand += this.RadGrid1_ItemCommand;
    RadGrid1.PreRender += this.RadGrid1_PreRender;
    RadGrid1.HeaderContextMenu.ItemCreated += this.HeaderContextMenu_ItemCreated;
}
 
protected void CreateTelerikGrid()
{
    CreateDefaultTelerikGrid();
}
 
protected void CreateDefaultTelerikGrid()
{
    WireRadGridEvents();
 
    RadGrid1.Height = Unit.Pixel(600);
    RadGrid1.Width = Unit.Pixel(936);
    RadGrid1.AllowFilteringByColumn = true;
    RadGrid1.AllowPaging = true;
    RadGrid1.PageSize = 10;
    RadGrid1.AutoGenerateColumns = false;
    RadGrid1.ShowStatusBar = true;
    RadGrid1.ShowGroupPanel = true;
 
    RadGrid1.PagerStyle.AlwaysVisible = true;
    RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
 
    RadGrid1.ClientSettings.AllowDragToGroup = true;
    RadGrid1.ClientSettings.Scrolling.AllowScroll = true;
    RadGrid1.ClientSettings.Scrolling.FrozenColumnsCount = 4;
    RadGrid1.ClientSettings.Scrolling.SaveScrollPosition = true;
    RadGrid1.ClientSettings.Scrolling.UseStaticHeaders = true;
 
    RadGrid1.MasterTableView.EnableHeaderContextMenu = true;
    RadGrid1.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
    RadGrid1.MasterTableView.CommandItemSettings.ShowExportToExcelButton = true;
    RadGrid1.MasterTableView.CommandItemSettings.ShowAddNewRecordButton = false;
    RadGrid1.MasterTableView.CommandItemSettings.RefreshText = "Restore Defaults";
 
    RadGrid1.MasterTableView.Columns.Clear();
    AddFixedColumns();
    AddTimeFrameColumns();
}
 
protected void AddFixedColumns()
{
    GridBoundColumn gbcGeography1 = new GridBoundColumn();
    gbcGeography1.DataField = "Geography1";
    gbcGeography1.HeaderText = "Geography1";
    gbcGeography1.UniqueName = "Geography1";
    gbcGeography1.Groupable = true;
    gbcGeography1.HeaderStyle.Width = Unit.Pixel(150);
    gbcGeography1.FilterTemplate = new ComboFilterTemplate(Page, "Geography1", "Geography1", Convert.ToInt32(gbcGeography1.HeaderStyle.Width.Value) - 7);
    RadGrid1.MasterTableView.Columns.Add(gbcGeography1);
 
    GridBoundColumn gbcGeography2 = new GridBoundColumn();
    gbcGeography2.DataField = "Geography2";
    gbcGeography2.HeaderText = "Geography2";
    gbcGeography2.UniqueName = "Geography2";
    gbcGeography2.Groupable = true;
    gbcGeography2.HeaderStyle.Width = Unit.Pixel(150);
    gbcGeography2.FilterTemplate = new ComboFilterTemplate(Page, "Geography2", "Geography2", Convert.ToInt32(gbcGeography2.HeaderStyle.Width.Value) - 7);
    RadGrid1.MasterTableView.Columns.Add(gbcGeography2);
 
    GridBoundColumn gbcDimensionGroup = new GridBoundColumn();
    gbcDimensionGroup.DataField = "DimensionGroup";
    gbcDimensionGroup.HeaderText = "DimensionGroup";
    gbcDimensionGroup.UniqueName = "DimensionGroup";
    gbcDimensionGroup.Groupable = true;
    gbcDimensionGroup.HeaderStyle.Width = Unit.Pixel(100);
    gbcDimensionGroup.FilterTemplate = new ComboFilterTemplate(Page, "DimensionGroup", "DimensionGroup", Convert.ToInt32(gbcDimensionGroup.HeaderStyle.Width.Value) - 7);
    RadGrid1.MasterTableView.Columns.Add(gbcDimensionGroup);
 
    GridBoundColumn gbcDimension = new GridBoundColumn();
    gbcDimension.DataField = "Dimension";
    gbcDimension.HeaderText = "Dimension";
    gbcDimension.UniqueName = "Dimension";
    gbcDimension.Groupable = true;
    gbcDimension.HeaderStyle.Width = Unit.Pixel(100);
    gbcDimension.FilterTemplate = new ComboFilterTemplate(Page, "Dimension", "Dimension", Convert.ToInt32(gbcDimension.HeaderStyle.Width.Value) - 7);
    RadGrid1.MasterTableView.Columns.Add(gbcDimension);
}
 
public class ComboFilterTemplate : ITemplate
{
    private RadComboBox combo;
    private string columnName;
    protected string field;
    protected String connectionString;
    protected Page page;
    protected int controlWidth;
 
    public ComboFilterTemplate(Page Page, string ColumnName, string Field, int Width)
    {
        field = Field;
        columnName = ColumnName;
        page = Page;
        controlWidth = Width;
    }
 
    public void InstantiateIn(Control container)
    {
        combo = new RadComboBox();
        combo.ID = String.Format("RadComboBox{0}", columnName);
        combo.AppendDataBoundItems = true;
        if (field.Length > 0)
            combo.DataTextField = field;
        combo.DataValueField = field;
        combo.Items.Insert(0, new RadComboBoxItem("All"));
        combo.DataBound += combo_DataBound;
        combo.DataBinding += new EventHandler(combo_DataBinding);
        combo.OnClientSelectedIndexChanged = String.Format("ClientComboFilterSelected_{0}", columnName);
        combo.Width = Unit.Pixel(controlWidth);
        container.Controls.Add(combo);
    }
 
    void combo_DataBinding(object sender, EventArgs e)
    {
        combo.DataSource = GetFilterDataSource(columnName);
    }
 
    void combo_DataBound(object sender, EventArgs e)
    {
        RadComboBox combo = (RadComboBox)sender;
        GridItem container = (GridItem)combo.NamingContainer;
 
        string script = "function ClientComboFilterSelected_" + columnName + "(sender,args) {var tableView=$find(\""
            + ((GridItem)container).OwnerTableView.ClientID + "\");tableView.filter(\""
            + field + "\",args.get_item().get_value(),\"EqualTo\");}";
 
        ScriptManager.RegisterStartupScript(page, page.GetType(), String.Format("ClientComboFilterSelected_{0}", field), script, true);
        combo.SelectedValue = container.OwnerTableView.GetColumn(columnName).CurrentFilterValue;
    }
}
 
protected void AddTimeFrameColumns()
{
    GetTimeFrames();
 
    foreach (string timeframe in timeframes)
    {
        GridBoundColumn gbcTimeFrame = new GridBoundColumn();
        RadGrid1.MasterTableView.Columns.Add(gbcTimeFrame);
        gbcTimeFrame.DataField = timeframe;
        gbcTimeFrame.HeaderText = timeframe;
        gbcTimeFrame.Groupable = false;
        gbcTimeFrame.AllowFiltering = false;
        gbcTimeFrame.HeaderStyle.Width = Unit.Pixel(100);
    }
 
}
 
protected void GetTimeFrames()
{
    timeframes.Add("Timeframe1");
    timeframes.Add("Timeframe2");
    timeframes.Add("Timeframe3");
    timeframes.Add("Timeframe4");
    timeframes.Add("Timeframe5");
    timeframes.Add("Timeframe6");
}
 
protected void RadGrid1_PreRender(object sender, System.EventArgs e)
{
    if (RadGrid1.MasterTableView.FilterExpression != string.Empty)
    {
        RefreshCombos();
    }
}
 
protected void RefreshCombos()
{
}
 
protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    RadGrid1.DataSource = RadGrid1DataSource();
}
 
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridFilteringItem)
    {
        GridFilteringItem gridfilteringitem = (GridFilteringItem)e.Item;
 
        try
        {
            RadComboBox radcomboboxgeography1 = (RadComboBox)gridfilteringitem.FindControl("RadComboBoxGeography1");
            radcomboboxgeography1.DataSource = GetFilterDataSource("Geography1");
            RadComboBox radcomboboxgeography2 = (RadComboBox)gridfilteringitem.FindControl("RadComboBoxGeography2");
            radcomboboxgeography2.DataSource = GetFilterDataSource("Geography2");
            RadComboBox radcomboboxdimensiongroup = (RadComboBox)gridfilteringitem.FindControl("RadComboBoxDimensionGroup");
            radcomboboxdimensiongroup.DataSource = GetFilterDataSource("DimensionGroup");
            RadComboBox radcomboboxdimension = (RadComboBox)gridfilteringitem.FindControl("RadComboBoxDimension");
            radcomboboxdimension.DataSource = GetFilterDataSource("Dimension");
        }
        catch
        {
        }
    }
}
 
protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
    if (e.CommandName == "RebindGrid")
    {
        CreateDefaultTelerikGrid();
    }
    else if (e.CommandName == "ExportToExcel")
    {
        RadGrid1.ExportSettings.ExportOnlyData = true;
        RadGrid1.ExportSettings.IgnorePaging = true;
        RadGrid1.ExportSettings.OpenInNewWindow = true;
        RadGrid1.ExportSettings.Excel.Format = GridExcelExportFormat.ExcelML;
        RadGrid1.MasterTableView.ExportToExcel();
    }
}
 
private void HeaderContextMenu_ItemCreated(object sender, RadMenuEventArgs e)
{
    if (e.Item.Value.Contains("Geography1") || e.Item.Value.Contains("Geography2") || e.Item.Value.Contains("DimensionGroup") || e.Item.Value.Contains("Dimension"))
    {
        e.Item.Visible = false;
    }
}
 
private System.Data.DataTable RadGrid1DataSource()
{
    System.Data.DataTable datatable = new System.Data.DataTable();
 
    datatable.Columns.Add("Geography1", typeof(string));
    datatable.Columns.Add("Geography2", typeof(string));
    datatable.Columns.Add("DimensionGroup", typeof(string));
    datatable.Columns.Add("Dimension", typeof(string));
    datatable.Columns.Add("Timeframe1", typeof(string));
    datatable.Columns.Add("Timeframe2", typeof(string));
    datatable.Columns.Add("Timeframe3", typeof(string));
    datatable.Columns.Add("Timeframe4", typeof(string));
    datatable.Columns.Add("Timeframe5", typeof(string));
    datatable.Columns.Add("Timeframe6", typeof(string));
 
    datatable.Rows.Add("Maryland", "Montgomery County", "Sex", "Male", new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100));
    datatable.Rows.Add("Maryland", "Montgomery County", "Sex", "Female", new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100));
    datatable.Rows.Add("Maryland", "Montgomery County", "Race", "White", new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100));
    datatable.Rows.Add("Maryland", "Montgomery County", "Race", "Black", new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100));
    datatable.Rows.Add("Maryland", "Montgomery County", "Race", "Asian", new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100));
    datatable.Rows.Add("Maryland", "Montgomery County", "Race", "Other", new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100));
    datatable.Rows.Add("Maryland", "Howard County", "Sex", "Male", new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100));
    datatable.Rows.Add("Maryland", "Howard County", "Sex", "Female", new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100));
    datatable.Rows.Add("Maryland", "Howard County", "Race", "White", new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100));
    datatable.Rows.Add("Maryland", "Howard County", "Race", "Black", new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100));
    datatable.Rows.Add("Maryland", "Howard County", "Race", "Asian", new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100));
    datatable.Rows.Add("Maryland", "Howard County", "Race", "Other", new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100));
    datatable.Rows.Add("North Carolina", "Wake County", "Sex", "Male", new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100));
    datatable.Rows.Add("North Carolina", "Wake County", "Sex", "Female", new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100));
    datatable.Rows.Add("North Carolina", "Wake County", "Race", "White", new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100));
    datatable.Rows.Add("North Carolina", "Wake County", "Race", "Black", new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100));
    datatable.Rows.Add("North Carolina", "Wake County", "Race", "Asian", new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100));
    datatable.Rows.Add("North Carolina", "Wake County", "Race", "Other", new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100));
    datatable.Rows.Add("North Carolina", "Rutherford County", "Sex", "Male", new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100));
    datatable.Rows.Add("North Carolina", "Rutherford County", "Sex", "Female", new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100));
    datatable.Rows.Add("North Carolina", "Rutherford County", "Race", "White", new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100));
    datatable.Rows.Add("North Carolina", "Rutherford County", "Race", "Black", new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100));
    datatable.Rows.Add("North Carolina", "Rutherford County", "Race", "Asian", new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100));
    datatable.Rows.Add("North Carolina", "Rutherford County", "Race", "Other", new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100), new Random().Next(0, 100));
 
    return datatable;
}
 
private static System.Data.DataTable GetFilterDataSource(string columnname)
{
    System.Data.DataTable datatable = new System.Data.DataTable();
 
    if (columnname == "Geography1")
    {
        datatable.Columns.Add("Geography1", typeof(string));
        datatable.Rows.Add("Maryland");
        datatable.Rows.Add("North Carolina");
    }
    else if (columnname == "Geography2")
    {
        datatable.Columns.Add("Geography2", typeof(string));
        datatable.Rows.Add("Montgomery County");
        datatable.Rows.Add("Howard County");
        datatable.Rows.Add("Wake County");
        datatable.Rows.Add("Rutherford County");
    }
    else if (columnname == "DimensionGroup")
    {
        datatable.Columns.Add("DimensionGroup", typeof(string));
        datatable.Rows.Add("Sex");
        datatable.Rows.Add("Race");
    }
    else if (columnname == "Dimension")
    {
        datatable.Columns.Add("Dimension", typeof(string));
        datatable.Rows.Add("Male");
        datatable.Rows.Add("Female");
        datatable.Rows.Add("White");
        datatable.Rows.Add("Black");
        datatable.Rows.Add("Asian");
        datatable.Rows.Add("Other");
    }
 
    return datatable;
}


1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 25 Apr 2013, 07:16 AM
Hi,

I got an error on trying your code. Try removing the '!IsPostBack' condition from your OnInit event.

Thanks,
Princy.
Tags
Grid
Asked by
Matt
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or