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

Rebind() not calling _NeedDataSource

1 Answer 74 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dave
Top achievements
Rank 1
Dave asked on 23 Jan 2014, 05:03 PM
I have a grid that's using the following settings:

<telerik:RadGrid ID="RadGridTest"
            runat="server"
            AllowPaging="True"
            AllowCustomPaging="True"
            GridLines="None"
            OnNeedDataSource="RadGridTest_NeedDataSource"
            PageSize="25"
            OnItemDataBound="RadGridTest_ItemDataBound"
            AutoGenerateColumns="false"
            EnableHeaderContextMenu="true"
            EnableLinqExpressions="false"
            EnableHeaderContextFilterMenu="true"
            AllowSorting="True"
            MasterTableView-AllowCustomSorting="true"
            OnSortCommand="RadGridTest_SortCommand"
            MasterTableView-AllowMultiColumnSorting="true"
            AllowFilteringByColumn="true">
    <PagerStyle Mode="NextPrevAndNumeric/>
    <HeaderContextMenu OnItemClick="HeaderContextMenu_ItemClick">
    </HeaderContextMenu>
    <ClientSettings AllowColumnHide="True">
        <ClientEvents OnGridCreated="GetGridObject"></ClientEvents>
    </ClientSettings>
</telerik:RadGrid>

When sorting using the header context menu, unlike just clicking on the header, no SortCommand event is fired. I found a thread where you recommended using the ItemClick event on the header.

The problem is that this event is called after the grid rebinds once you click it, and even if you explicitely call RadGridTest.Rebind() in the event it doesn't take it. The only solution I've found so far is to set the datasource to null and then call rebind.

// Doesn't change the grid at all
protected void HeaderContextMenu_ItemClick(object sender, RadMenuEventArgs e)
{
    if (e.Item.Text == "Clear Sorting")
    {
        string columnName = e.Item.Attributes["ColumnName"]; // ["columnUniqueName"] Returns Null!?
        // Prepare conditions for sorted data
        RadGridTest.Rebind(); // Never actually raises NeedDataSource
    }
}
 
// Updates the grid with the correct changes.
protected void HeaderContextMenu_ItemClick(object sender, RadMenuEventArgs e)
{
    if (e.Item.Text == "Clear Sorting")
    {
        string columnName = e.Item.Attributes["ColumnName"]; // ["columnUniqueName"] Returns Null!?
        // Prepare conditions for sorted data
        RadGridTest.DataSource = null;
        RadGridTest.Rebind();
    }
}

Is setting the datasource to null the correct approach, or is there something I am missing.

1 Answer, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 24 Jan 2014, 04:41 AM
Hi Dave,

I guess you are trying to access the column names in the HeaderContextMenu, please try the following code snippet:

ASPX:
<HeaderContextMenu OnItemClick="HeaderContextMenu_ItemClick" OnClientItemClicking="contextMenuItemClicking">
</HeaderContextMenu>
 
<ClientSettings>
 <ClientEvents OnColumnContextMenu="columnContextMenu" />
</ClientSettings>

C#:
protected void Page_Load(object sender, EventArgs e)
{
    RadGridTest.HeaderContextMenu.PreRender += new EventHandler(HeaderContextMenu_PreRender);    
}   
void HeaderContextMenu_PreRender(object sender, EventArgs e)
{
    RadMenuItem newItem = new RadMenuItem("Clear Sorting");
    newItem.Attributes["TableID"] = RadGridTest.UniqueID;
    RadGridTest.HeaderContextMenu.Items.Add(newItem);
}
protected void HeaderContextMenu_ItemClick(object sender, RadMenuEventArgs e)
{
    string tableID = e.Item.Attributes["TableID"];
    if (e.Item.Text == "Clear Sorting")
    {
        string columnName = e.Item.Attributes["ColumnName"]; // Get the column name
        RadGridTest.Rebind();
    }
}

JS:
<script type="text/javascript">
    var columnName;
    function columnContextMenu(sender, args) {
        columnName = args.get_gridColumn().get_uniqueName();
    }
 
    function contextMenuItemClicking(sender, args) {
        if (args.get_item().get_text() === "Clear Sorting") {
            sender.trackChanges();
            args.get_item().get_attributes().setAttribute("ColumnName", columnName);
            sender.commitChanges();
        }
    }
</script>

Thanks,
Princy
Tags
Grid
Asked by
Dave
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or