Maintaining DataSource when using paging and sorting

4 posts, 1 answers
  1. Matthew
    Matthew avatar
    13 posts
    Member since:
    Aug 2014

    Posted 18 Aug 2014 Link to this post

    Hello everyone,

    I am using a radGrid to display records of data. I am attempting to implement a RadDropDownList that will alter the displayed data in radGrid based on the value selected. The dropDown list correctly rebinds the data and displays the new set of information. My problem occurs when the user attempts to move to a different page or attempts to sort the information. The grid is rebound to the original dataSource and does not maintain the dropDown selected dataSource. What is the best way to maintain the correct dataSource through page postbacks?

    DropDown ASPX:
    <telerik:RadDropDownList ID="BatchRadDropDownList" runat="server" OnItemSelected="BatchNameSelected"
        AutoPostBack="true" DefaultMessage="- Batch Names -">
    </telerik:RadDropDownList>

    RadGrid NeedDataSource:
    protected void RadGridActionItem_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        // Provides RadGrid with Action Item data.
     
        List<IDataParameter> Parms = null;
        string SQL = string.Empty;
        DataTable dt;
     
        try
        {
            using (DataAccess da = new DataAccess())
            {
                SQL = COMPLIANCE_SQL.GET_ALL_NEW_ACTION_ITEMS();
                dt = da.GetDataSet(SQL, Parms).Tables[0];
                RadGridActionItem.DataSource = dt;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    BatchNameSelected (Rebind of DataSource to selected data):
    protected void BatchNameSelected(object sender, DropDownListEventArgs e)
            {
                List<IDataParameter> Parms = null;
                string SQL = string.Empty;
                DataTable dt;
     
                try
                {
                    // Query for batch names and bind data to Batch RadDropDownList.
                    using (DataAccess da = new DataAccess())
                    {
                        SQL = COMPLIANCE_SQL.GET_ACTION_ITEM_VIA_BATCH_NAME(ref Parms, e.Text);
                        dt = da.GetDataSet(SQL, Parms).Tables[0];
                        RadGridActionItem.DataSource = dt;
                        RadGridActionItem.Rebind();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

    RadGrid PageIndexChanged:
    protected void RadGridActionItem_PageIndexChanged(object sender, GridPageChangedEventArgs e)
    {
        // Allows for RadGrid paging property.
        RadGridActionItem.CurrentPageIndex = e.NewPageIndex;
    }


    Page_Load:
    protected void Page_Load(object sender, EventArgs e)
    {
        FillBatchDropDown();
    }

    Question: How do I maintain the selected DataSource through postbacks caused by paging and sorting?

    I have been unable to find a solution that pertains to my example and would appreciate any help I can get.

    Thanks,
    Matt
  2. Answer
    Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 18 Aug 2014 in reply to Matthew Link to this post

    Hi,

    Please try with the below code snippet.

    protected void RadGridActionItem_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        BindGrid();
    }
     
    protected void BatchNameSelected(object sender, DropDownListEventArgs e)
    {
        RadGridActionItem.Rebind();
    }
     
    protected void BindGrid()
    {
        List<IDataParameter> Parms = null;
        string SQL = string.Empty;
        DataTable dt;
     
        try
        {
            // Query for batch names and bind data to Batch RadDropDownList.
            using (DataAccess da = new DataAccess())
            {
                if (BatchRadDropDownList.SelectedIndex != -1)
                {
                    SQL = COMPLIANCE_SQL.GET_ACTION_ITEM_VIA_BATCH_NAME(ref Parms, e.Text);
                }
                else
                {
                    SQL = COMPLIANCE_SQL.GET_ALL_NEW_ACTION_ITEMS();
                }
     
                dt = da.GetDataSet(SQL, Parms).Tables[0];
                RadGridActionItem.DataSource = dt;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    Let me know if any concern.

    Thanks,
    Jayesh Goyani
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 18 Aug 2014 in reply to Jayesh Goyani Link to this post

    Please replace below code in above code snippet.

     COMPLIANCE_SQL.GET_ACTION_ITEM_VIA_BATCH_NAME(ref Parms, BatchRadDropDownList.Text)
    //OR
     COMPLIANCE_SQL.GET_ACTION_ITEM_VIA_BATCH_NAME(ref Parms, BatchRadDropDownList.SelectedItem.Text)
  5. Matthew
    Matthew avatar
    13 posts
    Member since:
    Aug 2014

    Posted 18 Aug 2014 Link to this post

    Jayesh, your solution works flawlessly. Thanks for the help!
Back to Top