Paging not working for dynamic radgrid

7 posts, 1 answers
  1. Andy
    Andy avatar
    5 posts
    Member since:
    Jul 2013

    Posted 03 Aug 2013 Link to this post

    I have couple of issues related to dynamically created Radgrid.
    Background: I have created a dynamic Radgrid which contains Templatecolumn in it, this template columns hold three dropdown list controls which work in a cascading manner. I'm pre-selecting the drop down and for new records its getting default selections. Everything works fine until I get into paging.

    Issues: 1. Whenever I use paging the page number is selected in the grid but it always shows first page records.
                2. The selections made in previous page dropdown list are not persisting during the paging.

    I'm not sure if both are related to same thing. Here is my code for your reference. Your help is highly appreciated.

     
    protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
             AddGrid();
            }
     
     private void AddGrid()
            {
     
            _grid = new RadGrid
                {
                    ID = "TestGrid",
                    PageSize = 30,
                    ShowGroupPanel = false,
                    AllowSorting = false,
                    AllowPaging = true,
                    AutoGenerateColumns = false,
                    GridLines = GridLines.Vertical,
                    VirtualItemCount =0               
                };
     
            _grid.ClientSettings.AllowDragToGroup = false;
                 _grid.ClientSettings.AllowColumnsReorder = false;
                 _grid.ClientSettings.AllowColumnHide = true;
                 _grid.ClientSettings.Animation.AllowColumnReorderAnimation = false;
                  _grid.ClientSettings.Animation.AllowColumnRevertAnimation = false;
     
                  _grid.MasterTableView.DataKeyNames = new[] { "Id" };
                  _grid.MasterTableView.ShowHeadersWhenNoRecords = true;
                  _grid.MasterTableView.UseAllDataFields = true;
                  _grid.MasterTableView.Name = "Master";
               _grid.ItemDataBound += GridItemDataBound;
     _grid.PageIndexChanged += new GridPageChangedEventHandler_grid_PageIndexChanged);
    _grid.NeedDataSource += new GridNeedDataSourceEventHandler(_grid_NeedDataSource);
     
     
     var countryTemplate= new GridTemplateColumn
                {
                    UniqueName = "ADDCountry",
                    HeaderText = "Country Field",
                    Resizable = true,
     
                };
                countryTemplate.HeaderStyle.Font.Bold = true;
                countryTemplate.ItemTemplate = new Templateclass(countryTemplate.UniqueName, ExcelColumns);
     
                _grid.MasterTableView.Columns.Add(sourceTemplate);
     
                 
                var stateTemplate = new GridTemplateColumn
                {
                    UniqueName = "ADDState",
                    HeaderText = "state Column Field",
                };
     
                stateTemplate .HeaderStyle.Font.Bold = true;
    stateTemplate .ItemTemplate = new Templateclass(stateTemplate .UniqueName, null);
     
                _grid.MasterTableView.Columns.Add(stateTemplate );
     
     
                var CityTemplate = new GridTemplateColumn
                {
                    UniqueName = "ADDCity",
                    HeaderText = "Mapping Rule",
                };
     
                CityTemplate .HeaderStyle.Font.Bold = true;
                CityTemplate .ItemTemplate = new Templateclass(CityTemplate .HeaderText, null);
     
                _grid.MasterTableView.Columns.Add(CityTemplate);
     
      Content.Controls.Add(_grid);
            }
     
    void _grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
            {
                ((RadGrid)sender).MasterTableView.VirtualItemCount = bindingObject.Count;
                ((RadGrid)sender).CurrentPageIndex = ((RadGrid)sender).MasterTableView.CurrentPageIndex;
                ((RadGrid)sender).DataSource = bindingObject;
                
            }
    void _grid_PageIndexChanged(object sender, GridPageChangedEventArgs e)
            {
                ((RadGrid)sender).MasterTableView.CurrentPageIndex = ((RadGrid)sender).CurrentPageIndex;
            }
     
     
     void GridItemCreated(object sender, GridItemEventArgs e)
            {
                if (e.Item is GridDataItem)
                {
                    GridDataItem dataItem = (GridDataItem)e.Item;
                    DropDownList ddlCountry= (DropDownList)dataItem.FindControl("ddlCountry");
     
                     
     
                    if (ddlCountry!= null)
                    {
                        ddlCountry.AutoPostBack = true;
     
                        ddlCountry.SelectedIndexChanged += new EventHandler(ddlCountry_SelectedIndexChanged);
     
     
                    }
                }
            }
     
    public class Templateclass: ITemplate
        {
    public Templateclass(string colNames,List<string> CountryNames)
            {
                colName = colNames;
                           countryNames= CountryNames;
            }
     
    public void InstantiateIn(Control container)
            {
    //create a drop down controls for template columns
    }
  2. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 05 Aug 2013 Link to this post

    Hello,

    I have tried with below code but not able to reproduce this issue.

    public partial class Forum : System.Web.UI.Page
    {
        RadGrid _grid;
     
        protected void Page_Init(object sender, System.EventArgs e)
        {
            AddGrid();
        }
     
        protected void Page_Load(object sender, System.EventArgs e)
        {
     
        }
     
        protected void Page_PreRender(object sender, System.EventArgs e)
        {
     
        }
     
        private void AddGrid()
        {
     
            _grid = new RadGrid
                {
                    ID = "TestGrid",
                    PageSize = 30,
                    ShowGroupPanel = false,
                    AllowSorting = false,
                    AllowPaging = true,
                    AutoGenerateColumns = false,
                    GridLines = GridLines.Vertical,
                    VirtualItemCount = 0,
                };
     
            _grid.PageSize = 2;
            _grid.ClientSettings.AllowDragToGroup = false;
            _grid.ClientSettings.AllowColumnsReorder = false;
            _grid.ClientSettings.AllowColumnHide = true;
            _grid.ClientSettings.Animation.AllowColumnReorderAnimation = false;
            _grid.ClientSettings.Animation.AllowColumnRevertAnimation = false;
     
            _grid.MasterTableView.DataKeyNames = new[] { "Id" };
            _grid.MasterTableView.ShowHeadersWhenNoRecords = true;
            _grid.MasterTableView.UseAllDataFields = true;
            _grid.MasterTableView.Name = "Master";
            _grid.ItemDataBound += new GridItemEventHandler(_grid_ItemDataBound);
            _grid.PageIndexChanged += new GridPageChangedEventHandler(_grid_PageIndexChanged);
            _grid.NeedDataSource += new GridNeedDataSourceEventHandler(_grid_NeedDataSource);
     
            GridBoundColumn column = new GridBoundColumn();
            column.DataField = "ID";
            column.HeaderText = "ID";
            column.UniqueName = "ID";
            _grid.MasterTableView.Columns.Add(column);
     
            column = new GridBoundColumn();
            column.DataField = "Name";
            column.HeaderText = "Name";
            column.UniqueName = "Name";
            _grid.MasterTableView.Columns.Add(column);
     
            this.form1.Controls.Add(_grid);
        }
     
        void _grid_ItemDataBound(object sender, GridItemEventArgs e)
        {
     
        }
     
        void _grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Rows.Add(1, "1");
            dt.Rows.Add(1, "2");
            dt.Rows.Add(1, "3");
            dt.Rows.Add(1, "4");
            dt.Rows.Add(1, "5");
            dt.Rows.Add(1, "6");
            dt.Rows.Add(1, "7");
     
            ((RadGrid)sender).MasterTableView.VirtualItemCount = dt.Rows.Count;
            ((RadGrid)sender).CurrentPageIndex = ((RadGrid)sender).MasterTableView.CurrentPageIndex;
            ((RadGrid)sender).DataSource = dt;
     
        }
        void _grid_PageIndexChanged(object sender, GridPageChangedEventArgs e)
        {
            ((RadGrid)sender).MasterTableView.CurrentPageIndex = ((RadGrid)sender).CurrentPageIndex;
        }
     
     
        void GridItemCreated(object sender, GridItemEventArgs e)
        {
     
        }
     
    }


    Thanks,
    Jayesh Goyani
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Andy
    Andy avatar
    5 posts
    Member since:
    Jul 2013

    Posted 05 Aug 2013 Link to this post

    Hey Jayesh, Thanks for the response, you are true in case of a bound column. I'm using a dynamic template column with dropdownlist  in it, ca n you please post a code snippet with this scenario. As I said earlier
    1. Whenever I use paging the page number is selected in the grid but it always shows first page records(dropdown list having first page selections).
    2. The selections made in previous page dropdown list are not persisting during the paging

    Looking forward for your response.
  5. Answer
    Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 06 Aug 2013 Link to this post

    Hello,

    I have added DropDownList in template column.

    Let me know if any concern.

    public partial class Forum : System.Web.UI.Page
    {
        RadGrid _grid;
     
        protected void Page_Init(object sender, System.EventArgs e)
        {
            AddGrid();
        }
        protected void Page_Load(object sender, System.EventArgs e)
        {
     
        }
        protected void Page_PreRender(object sender, System.EventArgs e)
        {
     
        }
     
        private void AddGrid()
        {
     
            _grid = new RadGrid
                {
                    ID = "TestGrid",
                    PageSize = 30,
                    ShowGroupPanel = false,
                    AllowSorting = false,
                    AllowPaging = true,
                    AutoGenerateColumns = false,
                    GridLines = GridLines.Vertical,
                    VirtualItemCount = 0,
                };
     
            _grid.PageSize = 2;
            _grid.ClientSettings.AllowDragToGroup = false;
            _grid.ClientSettings.AllowColumnsReorder = false;
            _grid.ClientSettings.AllowColumnHide = true;
            _grid.ClientSettings.Animation.AllowColumnReorderAnimation = false;
            _grid.ClientSettings.Animation.AllowColumnRevertAnimation = false;
     
            _grid.MasterTableView.DataKeyNames = new[] { "Id" };
            _grid.MasterTableView.ShowHeadersWhenNoRecords = true;
            _grid.MasterTableView.UseAllDataFields = true;
            _grid.MasterTableView.Name = "Master";
            _grid.ItemDataBound += new GridItemEventHandler(_grid_ItemDataBound);
            _grid.PageIndexChanged += new GridPageChangedEventHandler(_grid_PageIndexChanged);
            _grid.NeedDataSource += new GridNeedDataSourceEventHandler(_grid_NeedDataSource);
     
     
            GridBoundColumn column = new GridBoundColumn();
            column.DataField = "ID";
            column.HeaderText = "ID";
            column.UniqueName = "ID";
            _grid.MasterTableView.Columns.Add(column);
     
            column = new GridBoundColumn();
            column.DataField = "Name";
            column.HeaderText = "Name";
            column.UniqueName = "Name";
            _grid.MasterTableView.Columns.Add(column);
     
            List<string> CountryList = new List<string>();
            CountryList.Add("India");
            CountryList.Add("US");
     
            GridTemplateColumn test1 = new GridTemplateColumn();
            test1.HeaderText = "Template Column";
            test1.UniqueName = "TemplateColumn";
            test1.ItemTemplate = new MyTemplate("TemplateColumn", CountryList);
     
            _grid.MasterTableView.Columns.Add(test1);
     
            this.form1.Controls.Add(_grid);
        }
     
        void _grid_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = e.Item as GridDataItem;
                DropDownList _TemplateColumn = item.FindControl("TemplateColumn") as DropDownList;
                string strCountrName = (item.DataItem as DataRowView)["CountryName"].ToString();
     
                if (_TemplateColumn != null && _TemplateColumn.Items.FindByText(strCountrName) != null)
                {
                    _TemplateColumn.Items.FindByText(strCountrName).Selected = true;
                }
     
            }
        }
     
        void _grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("CountryName", typeof(string));
            dt.Rows.Add(1, "1", "US");
            dt.Rows.Add(1, "2", "India");
            dt.Rows.Add(1, "3", "India");
            dt.Rows.Add(1, "4", "India");
            dt.Rows.Add(1, "5", "India");
            dt.Rows.Add(1, "6", "India");
            dt.Rows.Add(1, "7", "India");
     
            ((RadGrid)sender).MasterTableView.VirtualItemCount = dt.Rows.Count;
            ((RadGrid)sender).CurrentPageIndex = ((RadGrid)sender).MasterTableView.CurrentPageIndex;
            ((RadGrid)sender).DataSource = dt;
     
        }
        void _grid_PageIndexChanged(object sender, GridPageChangedEventArgs e)
        {
            ((RadGrid)sender).MasterTableView.CurrentPageIndex = ((RadGrid)sender).CurrentPageIndex;
        }
     
     
        void GridItemCreated(object sender, GridItemEventArgs e)
        {
     
        }
    }
     
     
    public class MyTemplate : ITemplate
    {
     
        protected DropDownList boolValue;
        private string colname;
        protected List<string> CoutryList;
     
        public MyTemplate(string cName, List<string> CountryNames)
        {
            colname = cName;
            CoutryList = CountryNames;
        }
     
        public void InstantiateIn(System.Web.UI.Control container)
        {
            boolValue = new DropDownList();
            boolValue.ID = colname;
            boolValue.Width = Unit.Pixel(200);
            container.Controls.Add(boolValue);
     
            boolValue.DataSource = CoutryList;
            boolValue.DataBind();
        }
    }


    Thanks,
    Jayesh Goyani
  6. Andy
    Andy avatar
    5 posts
    Member since:
    Jul 2013

    Posted 07 Aug 2013 Link to this post

    Thank you Jayesh.
  7. bharath
    bharath avatar
    23 posts
    Member since:
    Jun 2011

    Posted 08 Sep 2014 in reply to Jayesh Goyani Link to this post

    my requirement is add textbox to radgrid columns and that text box should act as auto complete extender.
    My radgrid and textbox are dynamic..so i don't know which column  having textbox and bind that textbox to autocomplete extender at design time.

       provide some example to do this.

    /Tahnks
    Bharath
  8. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 10 Sep 2014 in reply to bharath Link to this post

    Hi,

    Can you please provide some part of your code?

    Thanks,
    Jayesh Goyani
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017