using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Layout;
using DevExpress.ExpressApp.Model.NodeGenerators;
using DevExpress.ExpressApp.SystemModule;
using DevExpress.ExpressApp.Templates;
using DevExpress.ExpressApp.Utils;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.Validation;
using DevExpress.ExpressApp.Web.Editors;
using DevExpress.ExpressApp.Web.Editors.ASPx;
using DevExpress.Web;
using DevExpress.ExpressApp.Model;
using DevExpress.Web.Data;

namespace Nuvolar.Module.Web.CSharp.Controllers
{
    // For more typical usage scenarios, be sure to check out https://documentation.devexpress.com/eXpressAppFramework/clsDevExpressExpressAppViewControllertopic.aspx.
    public partial class ASPxGridList_VC : ViewController<ListView>
    {
        public ASPxGridList_VC()
        {
            InitializeComponent();
            // Target required Views (via the TargetXXX properties) and create their Actions.
        }
        protected override void OnActivated()
        {
            base.OnActivated();
            // Perform various tasks depending on the target View.
        }
        protected override void OnViewControlsCreated()
        {
            base.OnViewControlsCreated();
            ASPxGridListEditor _ASPxGridListEditor = (View as ListView).Editor as ASPxGridListEditor;
            if (_ASPxGridListEditor!=null)
            {
                ASPxGridView _ASPxGridView = _ASPxGridListEditor.Grid as ASPxGridView;
                if (_ASPxGridView!=null)
                {
                    _ASPxGridView.CustomColumnDisplayText += ASPxGridView_CustomColumnDisplayText;
                    //_ASPxGridView.ParseValue += ASPxGridView_ParseValue;
                }
            }
        }

        private void ASPxGridView_ParseValue(object sender, ASPxParseValueEventArgs e)
        {
            if (e.FieldName=="")
            {
                try
                {

                }
                catch (Exception ex)
                {

                    throw;
                }
            }
            
        }

        private void ASPxGridView_CustomColumnDisplayText(object sender, ASPxGridViewColumnDisplayTextEventArgs e)
        {
            IModelColumnExtender _IModelColumnExtender = null;
            if (View!=null)
            {
                if (e.Column.FieldName!="")
                {
                    _IModelColumnExtender = View.Model.Columns[e.Column.FieldName] as IModelColumnExtender;
                    if (_IModelColumnExtender!=null)
                    {
                        if (_IModelColumnExtender.HideValueIfZero)
                        {
                            if (IsNumeric(e.Value))
                            {
                                if (double.Parse(e.Value.ToString())==0)
                                {
                                    e.DisplayText = "";
                                }
                                
                            }
                        }
                    }
                }
            }
        }
        private bool IsNumeric(object Expression)
        {
            double retNum;

            bool isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);
            return isNum;
        }
        protected override void OnDeactivated()
        {
            // Unsubscribe from previously subscribed events and release other references and resources.
            base.OnDeactivated();
        }
    }
}
