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

Filtering RadGrid will filled from DataTable

1 Answer 102 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Eric Klein
Top achievements
Rank 1
Eric Klein asked on 06 Oct 2011, 02:30 PM
I have a screen with a radgrid that is filled using the NeedDataSource Event
protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
       {
           int count = 0;
           if (RadGrid1.MasterTableView.SortExpressions.Count > 0)
               RadGrid1.DataSource = GetEntities(RadGrid1.PageSize, RadGrid1.MasterTableView.CurrentPageIndex * RadGrid1.PageSize, RadGrid1.MasterTableView.FilterExpression,
                  RadGrid1.MasterTableView.SortExpressions[0].FieldName + " " + RadGrid1.MasterTableView.SortExpressions[0].SortOrderAsString(), out count);
           else
               RadGrid1.DataSource = GetEntities(RadGrid1.PageSize, RadGrid1.MasterTableView.CurrentPageIndex * RadGrid1.PageSize, RadGrid1.MasterTableView.FilterExpression, "", out count);
           RadGrid1.MasterTableView.VirtualItemCount = count;
       }
       private DataTable GetEntities(int maximumRows, int startRowIndex, string filterExpression, string sortExpression, out int count)
       {
           ClientDataContext db = new ClientDataContext();
           var query = from g in db.GovtEntityLogs
                       select new
                       {
                           g.GovtEntityLogID,
                           g.ClientID,
                           g.Client.ClientName,
                           g.EntityName,
                           g.StartDate,
                           g.EndDate,
                           g.InvestorTypeID,
                           g.InvestorType.InvestorType1,
                       };
           //Set filter expresion
           if (!String.IsNullOrEmpty(filterExpression))
           {
               query = query.Where(String.Format(@"{0}", filterExpression));
           }
           if (!String.IsNullOrEmpty(sortExpression))
           {
               query = query.OrderBy(sortExpression);
           }
           else
           {
               query = query.OrderBy("ClientName");
           }
           //Set the total row count
           count = query.Count();
           var pagedData = query.Skip(startRowIndex).Take(maximumRows);
           DataTable dt = new DataTable();
           dt = pagedData.ToADOTable();
           return dt;
       }

This works fine as long as I have the page setting
<%@ Page Title="" EnableViewState="false"

but when I Filter from multilpe columns it only sorts the last column selected.  When I change the EnableViewState to TRUE, I get an error in the Filter in Dynamic.cs
"No applicable indexer exists in type '<>f__AnonymousType50`12'"
When I look at teh filter expression it shows

"(Convert.ToString(it[\"ClientName\"]).ToUpper() = \"Capital Investment Advisory Services, LLC (CIAS)\".ToUpper())"

Am I missing something?


1 Answer, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 10 Oct 2011, 03:43 AM
Hello Eric,

When wit disabled ViewState try setting the grid DataSource to null in the ItemCommand event:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if(e.CommandName == "Page" || e.CommandName == "Filter" || e.CommandName == "Sort")
    {
        RadGrid1.DataSource = null;
    }
}


Also, see if setting EnableLinqExpressions to false makes any difference.

Best wishes,
Iana Tsolova
the Telerik team
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 RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Eric Klein
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Share this question
or