How do you set default ordering for custom tables?

HelenaG Grulichova asked on September 11, 2012 07:31

How do you set default ordering for custom tables?

Correct Answer

HelenaG Grulichova answered on September 11, 2012 07:31

To set a different default order for your custom tables you need to modify a system file, so please do it carefully and make a backup first.

Let's say you have a title field in your custom table according to which you want to order your items by default. You would first open the \CMSModules\CustomTables\Controls\CustomTableDataList.ascx.cs file and find the HasItemOrderField method. Create a new similar method but for your custom field:

public bool HasTitleField
{
get
{
if (FormInfo != null)
{
return (FormInfo.GetFormField("title") != null);
}
else
{
// If form info is not available assume ItemOrder is not present to prevent further exceptions
return false;
}
}
}

Then, scroll down to where the Page_Load method ends and update the code to be similar as follows:

if (HasTitleField)
{
gridData.OrderBy = "title DESC";
}
else if (HasItemOrderField)
{
gridData.OrderBy = "ItemOrder ASC";
}

-jh-
0 votesVote for this answer Unmark Correct answer

   Please, sign in to be able to submit a new answer.