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

Telerik Grid for ASP.Net ( "Show All" option on Pager causes error after insert)

5 Answers 123 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Radhika
Top achievements
Rank 1
Radhika asked on 21 Jan 2014, 07:08 PM
 When I enable the option to display "ALL" records int he grid, it is working. But when I add one more item with the ALL option selected, it throws an error.
Following code is my workaround. With this code, with ALL selected, when I add a new item, it goes to the second page, though all items shouls fit in the first page itself ( I have less than 50 items).

protected

 

 

void rgProjects_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)

 

{


 

 

if (e.Item is GridPagerItem)

 

{

 

 

var dropDown = (RadComboBox)e.Item.FindControl("PageSizeComboBox");

 

 

 

var totalCount = ((GridPagerItem)e.Item).Paging.DataSourceCount;

 

 

 

bool ALLSelected = (dropDown.SelectedItem.Text == "ALL") ? true : false;

 

 

 

var sizes = new Dictionary<string, string>()

 

{

{

 

"50", "50"},

 

{

 

"100", "100"},

 

{

 

"200", "200"}

 

};

sizes.Add(

 

"All", totalCount.ToString());

 

dropDown.Items.Clear();

 

 

foreach (var size in sizes)

 

{

 

 

var cboItem = new RadComboBoxItem()

 

{

Text = size.Key,

Value = size.Value

};

cboItem.Attributes.Add(

 

"ownerTableViewId", e.Item.OwnerTableView.ClientID);

 

dropDown.Items.Add(cboItem);

}

 

 

try

 

{

 

 

if (ALLSelected)

 

dropDown.FindItemByValue(totalCount.ToString()).Selected =

 

true;

 

 

 

else

 

{

 

 

RadComboBoxItem item = dropDown.FindItemByValue(rgProjects.PageSize.ToString());

 

 

 

if (item != null) item.Selected = true;

 

}

}

 

 

catch (Exception ex)

 

{

ShowValidationMessage(ex.Message);

}

}
}

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 22 Jan 2014, 04:46 AM
Hi Radhika,

Please try the following code snippet in the ItemCreated event of the Radgrid to customize the page size:

C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridPagerItem)
    {
        var dropDown = (RadComboBox)e.Item.FindControl("PageSizeComboBox");
        var totalCount = ((GridPagerItem)e.Item).Paging.DataSourceCount;
        var sizes = new Dictionary<string, string>() {
            {"10", "10"},
            {"20", "20"},
            {"50", "50"}
        };
        if (totalCount > 100)
        {
            sizes.Add("100", "100");
        }
        if (totalCount > 200)
        {
            sizes.Add("200", "200");
        }
        sizes.Add("All", totalCount.ToString());
 
        dropDown.Items.Clear();
        foreach (var size in sizes)
        {
            var cboItem = new RadComboBoxItem() { Text = size.Key, Value = size.Value };
            cboItem.Attributes.Add("ownerTableViewId", e.Item.OwnerTableView.ClientID);
            dropDown.Items.Add(cboItem);
        }
        dropDown.FindItemByValue(e.Item.OwnerTableView.PageSize.ToString()).Selected = true;
    }
}

Thanks,
Princy
0
Radhika
Top achievements
Rank 1
answered on 22 Jan 2014, 03:41 PM
Hello Princy,

Thank you for your response. I tested your piece of code in item created event. It shows the same behaviour. Steps to recreate:

1. Select "ALL" in the combobox.
2. Say there are 5 items in the list. You insert one more item.
3. Then , even though the selection says "ALL", it does not display all 6 items on the same page. It adds another page.

I think the reason for this is, when you add a new item, the editable row (with text boxes) is being counted as one item. So, totalcount is set to 6, but it thought the editable row is one of the items. So it shows only 5 items on fisrt page and bumps off the 6th item to next page.

It is not that critical so we are leaving the behaviour as it is.

Thanks.
Radhika
0
Jayesh Goyani
Top achievements
Rank 2
answered on 23 Jan 2014, 05:16 AM
sorry by mistake
0
Radhika
Top achievements
Rank 1
answered on 23 Jan 2014, 01:37 PM
Hello,

Thank you all for your responses.
I ended up using the following code in the ItemCreated event to fix the issue with inserting new rows when "ALL" is selected in paging.
Basically, I am setting the page size when there is a mismatch.

e.Item.OwnerTableView.PageSize = totalCount;



 

 

if (e.Item is GridPagerItem)

 

{

 

 

var dropDown = (RadComboBox)e.Item.FindControl("PageSizeComboBox");

 

 

 

int currentPageSize = e.Item.OwnerTableView.PageSize;

 

 

 

var totalCount = ((GridPagerItem)e.Item).Paging.DataSourceCount;

 

 

 

RadComboBoxItem item;

 

#region

 

 

"Fill the drop Down"

 

 

 

var sizes = new Dictionary<string, string>()

 

{

{

 

"50", "50"},

 

{

 

"100", "100"},

 

{

 

"200", "200"}

 

};

sizes.Add(

 

"ALL", totalCount.ToString());

 

dropDown.Items.Clear();

 

 

foreach (var size in sizes)

 

{

 

 

var cboItem = new RadComboBoxItem()

 

{

Text = size.Key,

Value = size.Value

};

cboItem.Attributes.Add(

 

"ownerTableViewId", e.Item.OwnerTableView.ClientID);

 

dropDown.Items.Add(cboItem);

}

#endregion

 

 

 

try

 

{

 

 

if (currentPageSize == totalCount) //Select "ALL" option ( When totalcount = 50, it shows "ALL" as selected instead of 50)

 

{

item = dropDown.FindItemByText(

 

"ALL");

 

}

 

 

else //find the item in dropdown

 

item = dropDown.FindItemByValue(currentPageSize.ToString());

 

 

if (item != null) item.Selected = true;

 

 

 

else //currentPageSize is not in the dropdown, means ALL was selected and total count changed either becasue of delete or insert

 

{

e.Item.OwnerTableView.PageSize = totalCount;

dropDown.FindItemByText(

 

"ALL").Selected = true;

 

}

}

 

 

catch (Exception ex)

 

{

ShowValidationMessage(ex.Message);

}

}

0
Viktor Tachev
Telerik team
answered on 27 Jan 2014, 09:50 AM
Hi,

There is a different thread discussing this functionality. It includes a sample project that illustrates the approach for adding "All" option on the pager combo box. You could check out the thread here.

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