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

RadGrid Custom Databinding, Sorting, Filtering & Paging without using NeedDataSource Event

2 Answers 708 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ananth Kumar
Top achievements
Rank 2
Ananth Kumar asked on 21 Jul 2011, 06:46 AM
Hello,

I have come across a special scenario in the web app i am developing where I need to implement custom databinding. That is i need to bind my radgrid without using its NeedDataSource Event. I want something like this (traditional ASP.NET GridView way) :
myRadGrid.DataSource= myDataSet;
myRadGrid.DataBind();
And because of this, I also need to implement custom sorting, filtering and paging. 
It will be very nice if you could post a sample app with the code implementing all of the above requirements.

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 21 Jul 2011, 07:17 AM
Hello Ananth,

In order to achieve this, you can use Simple data binding. For implementing operations like paging, filtering using simple data binding, you should call Rebind() method after each operation. Check the following help documentation which explains more about this.
Simple Data-binding.

For handling complex operations like paging, filtering etc, a better approach is to use Advanced data binding using NeedDataSource event. The following help documentation explains about this in detail.
Advanced Data-binding (using NeedDataSource event).

Thanks,
Shinu.
0
Ananth Kumar
Top achievements
Rank 2
answered on 21 Jul 2011, 09:51 AM
Dear Shinu,

Thanks for your reply.
I had already visited the link you gave and followed in my code, what exactly is shown there.   But, I dont see the Rebind(); method being used anywhere in the page.
I am getting the data displayed in my grid, but its not giving me the desired results while sorting. Even the sort direction image is not changing when i click it. Here is my code. could you please take a look @ it and tell me where i am wrong:

protected void rgGradeDetails_OnSortCommand(object sender, GridSortCommandEventArgs e)
        {
            //e.Canceled = true; //do I need to do something with this?
 
            RadGrid rg=sender as RadGrid;
 
            string grade = (e.Item.Parent.Parent.Parent.Parent.Parent.FindControl("hidGrade") as HiddenField).Value;
             
            DataTable dTab=GetGradeDetails(grade).ToTable();         
            DataView dView = dTab.DefaultView;
             
            if (e.NewSortOrder!=GridSortOrder.None)
            {
                dView.Sort ="["+ e.SortExpression + "] " + ((e.NewSortOrder == GridSortOrder.Ascending) ? "ASC" : "DESC");
            }
            rg.DataSource = dView;
// Actually I am using another function to retrive the data initially & bind columns based on that (pls see next code block). Should I call the same here?
            rg.DataBind();         
        }


protected void BindRgGradeDetails(RadGrid rgGradeDetails, string grade)
        {            
            DataTable dt = GetGradeDetails(grade).ToTable() ;      
 
            GridHyperLinkColumn hlnkColStudentId = new GridHyperLinkColumn();
            hlnkColStudentId.HeaderText = "Student Id";
            hlnkColStudentId.DataTextField = "Student Id";
            hlnkColStudentId.DataNavigateUrlFields = new string[] { "StudentId" };
            hlnkColStudentId.DataNavigateUrlFormatString = "../Details.aspx?StudentId={0}";
            hlnkColStudentId.AllowFiltering = false;
            rgGradeDetails.MasterTableView.Columns.Add(hlnkColStudentId);           
 
            GridBoundColumn gbcStudentName = new GridBoundColumn();
            gbcStudentName.DataType = typeof(System.String);
            gbcStudentName.HeaderText = "Student Name";
            gbcStudentName.DataField = "Student Name";
            gbcStudentName.AllowFiltering = true;
            gbcStudentName.AllowSorting = true;
            gbcStudentName.SortExpression = "Student Name";
            gbcStudentName.FilterControlWidth = new System.Web.UI.WebControls.Unit(60, UnitType.Percentage);
            rgGradeDetails.MasterTableView.Columns.Add(gbcStudentName);
 
            foreach (DataColumn col in dt.Columns)
            {
                if (col.ColumnName != "Student Id" && col.ColumnName != "StudentId" && col.ColumnName != "Current Grade" && col.ColumnName != "Current School" && col.ColumnName != "Student Name")
                {
                    GridBoundColumn gbc = new GridBoundColumn();
                    gbc.DataType = typeof(System.Double);
                   
                     gbc.HeaderText = col.ColumnName;
                     gbc.UniqueName = col.ColumnName;               
 
                    gbc.DataField = col.ColumnName;
                    gbc.AllowFiltering = true;
                    gbc.AllowSorting = true;
                    gbc.FilterControlWidth = new System.Web.UI.WebControls.Unit(60, UnitType.Percentage);
                    gbc.SortExpression = col.ColumnName;
                    rgGradeDetails.MasterTableView.Columns.Add(gbc);
                }
            }
            rgGradeDetails.DataSource = dt;
            rgGradeDetails.DataBind();
        }

The above codes aren't working.
I am not in a position to use the NeedDataSource event, as my columns are dynamically generated, and they cannot be determined even in the Page PreInit event. Could you please post a simple code focused on custom sorting using traditional databinding approach?

Many thanks in advance.
Tags
Grid
Asked by
Ananth Kumar
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
Ananth Kumar
Top achievements
Rank 2
Share this question
or