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

Maintaining DataSource when using paging and sorting

3 Answers 70 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Matthew
Top achievements
Rank 1
Matthew asked on 18 Aug 2014, 02:28 PM
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

3 Answers, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 18 Aug 2014, 07:48 PM
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
0
Jayesh Goyani
Top achievements
Rank 2
answered on 18 Aug 2014, 07:50 PM
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)
0
Matthew
Top achievements
Rank 1
answered on 18 Aug 2014, 08:04 PM
Jayesh, your solution works flawlessly. Thanks for the help!
Tags
Grid
Asked by
Matthew
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Matthew
Top achievements
Rank 1
Share this question
or