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

Using NeedDataSource for RadGrid, but sorting is not behaving!

4 Answers 1101 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Craig
Top achievements
Rank 1
Craig asked on 19 Feb 2014, 12:35 PM
Hi,
I am using NeedDataSource to populate a grid...  The following code (shown below) is used in my server control after the radGrid has been created.
I have followed the telerik examples, but had to add the "else" part of this statement as postbacks (eg from buttons unrelated to the grid) caused a blank grid.

Anyway assuming this is valid, I am getting strange behaviour when I sort the grid....
When I click the first column to sort, then click the second column to sort... the third column is actually sorted... If I click the second column again, the second column is then sorted as required...

eg..

My Grid - Unsorted
|  Col 1  |  Col 2  |  Col 3  |
     B            C           A
     C            A           D
     A            B           C
     D            D           B

Now I click Col 1 (All OK):
|  Col 1  |  Col 2  |  Col 3  |
     A            B           C
     B            C           A
     C            A           D
     D            D           B


But now I click Col 2 (But Col 3 sorts !)
|  Col 1  |  Col 2  |  Col 3  |
     B            C           A
     D            D           B
     A            B           C
     C            A           D

Only when I click Col 2 again does Col 2 actually sort correctly...

has anyone come across this issue beofre?

Here is my code 

            [radGrid1 is created here with all columns etc]
.
.
.
            radGrid1.NeedDataSource += new GridNeedDataSourceEventHandler(radGrid1_NeedDataSource);
            if (!Page.IsPostBack)
            {
                // A couple of lines for applying a default filter.
                radGrid1.MasterTableView.FilterExpression = "[Variation] = 'Committing'";
                GridColumn variationColumn = radGrid1.Columns.FindByUniqueName("Variation");
                variationColumn.CurrentFilterFunction = GridKnownFunction.EqualTo;
                variationColumn.CurrentFilterValue = "Committing";
                radGrid1.DataSource = GetData();
                radGrid1.DataBind();
            }
            else 
            {
                radGrid1.Rebind();
             }
.
.
.
        void radGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
                (sender as RadGrid).DataSource=GetData();  // Returns DataTable
        }


Thanks in advance!

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 19 Feb 2014, 01:08 PM
Hi Craig,

Please try the below sample code snippet. The sorting, filtering, paging etc. works fine with the use of NeedDataSource event.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" AllowPaging="true" AutoGenerateEditColumn="true" AllowSorting="true" AllowFilteringByColumn="true" EnableLinqExpressions="false" OnNeedDataSource="RadGrid1_NeedDataSource" OnPreRender="RadGrid1_PreRender">
    <MasterTableView DataKeyNames="OrderID">
        <Columns>
            <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID" />
            <telerik:GridBoundColumn DataField="ShipCity" HeaderText="ShipCity" UniqueName="ShipCity" />
            <telerik:GridBoundColumn UniqueName="EmployeeID" DataField="EmployeeID" HeaderText="EmployeeID" />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
   String ConnString = ConfigurationManager.ConnectionStrings["Northwind_newConnectionString3"].ConnectionString;
   SqlConnection conn = new SqlConnection(ConnString);
   SqlDataAdapter adapter = new SqlDataAdapter();
   adapter.SelectCommand = new SqlCommand("SELECT OrderID, ShipCity,EmployeeID FROM Orders", conn);
   DataTable myDataTable = new DataTable();
   conn.Open();
   try
   {
       adapter.Fill(myDataTable);
   }
   finally
   {
       conn.Close();
   }
   RadGrid1.DataSource = myDataTable;
}
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {
       RadGrid1.MasterTableView.FilterExpression = "([ShipCity] LIKE \'%London%\') ";
       GridColumn column = RadGrid1.MasterTableView.GetColumnSafe("ShipCity");
       column.CurrentFilterFunction = GridKnownFunction.Contains;
       column.CurrentFilterValue = "London";
       RadGrid1.MasterTableView.Rebind();
   }
}


Thanks,
Shinu
0
Craig
Top achievements
Rank 1
answered on 19 Feb 2014, 03:16 PM
Hi Shinu,

Thanks for the reply but for me this does not work... Could this be because my grid sits on the second tab of a TabStrip/Multipage?

Using the Rebind (if not a PostBack) is fine for the first request, but if I perform a postback action on Tab1, the grid disappears on the second tab...  Do I have to construct my code differently because it is in a Tab...

ps. NeedDataSource for my grid on Tab 2 does not fire if I post back when using button on Tab1.
0
Craig
Top achievements
Rank 1
answered on 19 Feb 2014, 03:52 PM
I have  got this working, although not perfectly....

I have had to remove the "if(!IsPostback)" so the rebind happens every time... It seems that using the Rebind() on CreateChildControls caused the odd sorting issue... moving this to Grid_PreRender solved this... Thanks
0
Accepted
Viktor Tachev
Telerik team
answered on 24 Feb 2014, 07:55 AM
Hello Craig,

In the code you provided in the first post it seems that you are using simple data binding (with DataBind() method) and also NeedDataSource event. Note that simple data binding is generally used when RadGrid is used in simple scenarios where operations like sorting, paging, filtering, etc. are not needed. Moreover you should never mix simple and advanced data binding.

In your scenario it is recommended to use advanced data binding. You should set the DataSource for the RadGrid only in the NeedDataSource event handler. Note that you should also remove all calls to DataBind() from the code and if you need to rebind the grid call the Rebind() method.

If you would like additional information on data binding with the NeedDataSource event it is available in this article.

Regards,
Viktor Tachev
Telerik
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 UI for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Craig
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Craig
Top achievements
Rank 1
Viktor Tachev
Telerik team
Share this question
or