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

Paging not working for dynamic radgrid

6 Answers 73 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Andy
Top achievements
Rank 1
Andy asked on 03 Aug 2013, 04:32 PM
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
}

6 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 05 Aug 2013, 08:58 AM
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
0
Andy
Top achievements
Rank 1
answered on 05 Aug 2013, 01:22 PM
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.
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 06 Aug 2013, 08:59 AM
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
0
Andy
Top achievements
Rank 1
answered on 07 Aug 2013, 09:45 PM
Thank you Jayesh.
0
bharath
Top achievements
Rank 1
answered on 08 Sep 2014, 11:31 PM
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
0
Jayesh Goyani
Top achievements
Rank 2
answered on 10 Sep 2014, 06:01 PM
Hi,

Can you please provide some part of your code?

Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Andy
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Andy
Top achievements
Rank 1
bharath
Top achievements
Rank 1
Share this question
or