Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
30
WebHierarchicalDataGrid disable/enable sorting issue
posted

We are using WebHierarchicalDataGrid to display user information.We want to disable/enable sorting based on a checkbox.

I have tried disabling the sorting using below ways:

1. By calling 

WebHierarchicalDataGrid.GridView.Behaviours.Sorting.Enabled = false.

This disabled sorting on parent.

For children:

void OnHierarchicalDataGridRowIslandDataBinding(object sender, RowIslandEventArgs e)
{
if (e.RowIsland.DataMember == "child1"  || e.RowIsland.DataMember == "child2")
{
 WebHierarchicalDataGrid WHDG = (WebHierarchicalDataGrid)sender;

 if (!WHDG.GridView.Behaviors.Sorting.Enabled)
 {
 e.RowIsland.Behaviors.Sorting.Enabled = false;
 }
 else if (!e.RowIsland.Behaviors.Sorting.Enabled)
 {
 e.RowIsland.Behaviors.Sorting.Enabled = true;
 }
}
}

2. 

void M_hierarchicalGrid_ColumnSorted(object sender, SortingEventArgs e)
{
if (!m_sortingEnabled)
e.SortedColumns.Clear();
}

This disables the sorting but:

The grid is initially displayed in descending order. I click on 1st column to sort in ascending order. All the rows are sorted.

I then click on checkbox to disable sorting. All the columns except the 1st are displayed in descending order. 

The data displayed in the grid, when sorting is displayed is incorrect. 

I have also saved the sorted column and direction and tried to apply it, but it did not work

Parents
No Data
Reply
  • 20255
    Offline posted

    Hello Vijay,

    Thank you for contacting us!

    The first approach that you mentioned should be enough to fulfill the desired requirement, but as I understand you want to invoke the enable/disable on checkbox click. Keep in mind that this is a dynamic change that should be handled appropriately by the application Lifecycle. For example, the right time to set Grids behaviors is on Page_Init or Load.

    With that said, another approach could be to invoke a postback on checkbox click, and disable the behavior on Page_Load/Init. The only tricky part would be to determine the postback control id, but this is achievable with the following method:

    private string getPostBackControlName()
    {
    	Control control = null;
    	//first we will check the "__EVENTTARGET" because if post back made by       the controls
    	//which used "_doPostBack" function also available in Request.Form collection.
    	string ctrlname = Page.Request.Params["__EVENTTARGET"];
    	if (ctrlname != null && ctrlname != String.Empty)
    	{
    		control = Page.FindControl(ctrlname);
    	}
    	// if __EVENTTARGET is null, the control is a button type and we need to
    	// iterate over the form collection to find it               
    	else
    	{
    		string ctrlStr = String.Empty;
    		Control c = null;
    		foreach (string ctl in Page.Request.Form)
    		{
    			//handle ImageButton they having an additional "quasi-property" in their Id which identifies
    			//mouse x and y coordinates
    			if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
    			{
    				ctrlStr = ctl.Substring(0, ctl.Length - 2);
    				c = Page.FindControl(ctrlStr);
    			}
    			else
    			{
    				c = Page.FindControl(ctl);
    			}
    			if (c is System.Web.UI.WebControls.Button ||
    					 c is System.Web.UI.WebControls.ImageButton)
    			{
    				control = c;
    				break;
    			}
    		}
    	}
    	return (control == null) ? "" : control.ID;
    
    }
    

    And now on Page_Init you can use the following check:

    string id = getPostBackControlName();
    
    if (!String.IsNullOrEmpty(id) && (id == "Button1"))
    {
     // Disable sorting behavior
    }
    

Children
No Data