/// <summary>
    /// Grid external data bound handler.
    /// </summary>
    protected object gridFiles_OnExternalDataBound(object sender, string sourceName, object parameter)
    {
        // Get the data row view from parameter
        DataRowView drv = null;

        if (parameter is DataRowView)
        {
            drv = (DataRowView)parameter;
        }
        else if (parameter is GridViewRow)
        {
            // Get data from the grid view row
            drv = (DataRowView)((GridViewRow)parameter).DataItem;
        }

        // Get the action button
        CMSGridActionButton btn = sender as CMSGridActionButton;

        switch (sourceName)
        {
        case "delete":
        {
            // Delete action
            int    siteId   = ValidationHelper.GetInteger(drv["MetaFileSiteID"], 0);
            string siteName = SiteInfoProvider.GetSiteName(siteId);

            Guid   guid      = ValidationHelper.GetGuid(drv["MetaFileGUID"], Guid.Empty);
            string extension = ValidationHelper.GetString(drv["MetaFileExtension"], "");

            // Check if the file is in DB
            bool db = ValidationHelper.GetBoolean(drv["HasBinary"], false);

            // Check if the file is in the file system
            bool   fs   = false;
            string path = MetaFileInfoProvider.GetFilePhysicalPath(siteName, guid.ToString(), extension);
            if (File.Exists(path))
            {
                fs = true;
            }

            // If the file is present in both file system and database, delete is allowed
            if (fs && db)
            {
                var locationType = GetFilesLocationType(siteId);

                // If the files are stored in file system or use both locations, delete is allowed in database
                if (locationType != FilesLocationTypeEnum.Database)
                {
                    btn.OnClientClick = btn.OnClientClick.Replace("'delete'", "'deleteindatabase'");
                    btn.ToolTip       = "Delete from database";
                    return(parameter);
                }

                // Files are stored in database, delete is allowed in file system
                btn.OnClientClick = btn.OnClientClick.Replace("'delete'", "'deleteinfilesystem'");
                btn.ToolTip       = "Delete from file system";
                return(parameter);
            }

            btn.Visible = false;
        }
        break;

        case "copy":
        {
            // Delete action
            int    siteId   = ValidationHelper.GetInteger(drv["MetaFileSiteID"], 0);
            string siteName = SiteInfoProvider.GetSiteName(siteId);

            Guid   guid      = ValidationHelper.GetGuid(drv["MetaFileGUID"], Guid.Empty);
            string extension = ValidationHelper.GetString(drv["MetaFileExtension"], "");

            // Check if the file is in DB
            bool db = ValidationHelper.GetBoolean(drv["HasBinary"], false);

            // Check if the file is in the file system
            bool   fs   = false;
            string path = MetaFileInfoProvider.GetFilePhysicalPath(siteName, guid.ToString(), extension);
            if (File.Exists(path))
            {
                fs = true;
            }

            var locationType = GetFilesLocationType(siteId);

            // If the file is stored in file system and the file is not present in database, copy to database is allowed
            if (fs && !db && (locationType == FilesLocationTypeEnum.Both))
            {
                btn.OnClientClick = btn.OnClientClick.Replace("'copy'", "'copytodatabase'");
                btn.ToolTip       = "Copy to database";
                //btn.ImageUrl =
                return(parameter);
            }
            // If the file is stored in database and the file is not present in file system, copy to file system is allowed
            if (db && !fs && (locationType != FilesLocationTypeEnum.Database))
            {
                btn.OnClientClick = btn.OnClientClick.Replace("'copy'", "'copytofilesystem'");
                btn.ToolTip       = "Copy to file system";
                return(parameter);
            }

            btn.Visible = false;
        }
        break;

        case "name":
        {
            // MetaFile name
            string name      = ValidationHelper.GetString(drv["MetaFileName"], "");
            Guid   guid      = ValidationHelper.GetGuid(drv["MetaFileGUID"], Guid.Empty);
            int    siteId    = ValidationHelper.GetInteger(drv["MetaFileSiteID"], 0);
            string extension = ValidationHelper.GetString(drv["MetaFileExtension"], "");

            // File name
            name = Path.GetFileNameWithoutExtension(name);

            string url = ResolveUrl("~/CMSPages/GetMetaFile.aspx?fileguid=") + guid;
            if (siteId != currentSiteId)
            {
                // Add the site name to the URL if not current site
                SiteInfo si = SiteInfoProvider.GetSiteInfo(siteId);
                if (si != null)
                {
                    url += "&sitename=" + si.SiteName;
                }
            }

            string tooltipSpan = name;
            bool   isImage     = ImageHelper.IsImage(extension);
            if (isImage)
            {
                int imageWidth  = ValidationHelper.GetInteger(DataHelper.GetDataRowViewValue(drv, "MetaFileImageWidth"), 0);
                int imageHeight = ValidationHelper.GetInteger(DataHelper.GetDataRowViewValue(drv, "MetaFileImageHeight"), 0);

                string tooltip = UIHelper.GetTooltipAttributes(url, imageWidth, imageHeight, null, name, extension, null, null, 300);
                tooltipSpan = "<span id=\"" + guid + "\" " + tooltip + ">" + name + "</span>";
            }

            return(UIHelper.GetFileIcon(Page, extension, tooltip: name) + "&nbsp;<a href=\"" + url + "\" target=\"_blank\">" + tooltipSpan + "</a>");
        }

        case "size":
            // File size
            return(DataHelper.GetSizeString(ValidationHelper.GetInteger(parameter, 0)));

        case "yesno":
            // Yes / No
            return(UniGridFunctions.ColoredSpanYesNo(parameter));

        case "site":
        {
            int siteId = ValidationHelper.GetInteger(parameter, 0);
            if (siteId > 0)
            {
                SiteInfo si = SiteInfoProvider.GetSiteInfo(siteId);
                if (si != null)
                {
                    return(si.DisplayName);
                }
            }
            return(null);
        }

        case "storedinfilesystem":
        {
            // Delete action
            int    siteId   = ValidationHelper.GetInteger(drv["MetaFileSiteID"], 0);
            string siteName = SiteInfoProvider.GetSiteName(siteId);

            Guid   guid      = ValidationHelper.GetGuid(drv["MetaFileGUID"], Guid.Empty);
            string extension = ValidationHelper.GetString(drv["MetaFileExtension"], "");

            // Check if the file is in DB
            bool db = ValidationHelper.GetBoolean(drv["HasBinary"], false);

            // Check if the file is in the file system
            bool   fs   = false;
            string path = MetaFileInfoProvider.GetFilePhysicalPath(siteName, guid.ToString(), extension);
            if (File.Exists(path))
            {
                fs = true;
            }

            return(UniGridFunctions.ColoredSpanYesNo(fs));
        }
        }

        return(parameter);
    }