C#/winforms: как лучше всего связать propertygrid и Систему. Данные. DataRow

Ах, может быть, нет класса, чтобы сделать это, но был вопрос по коду для гольфа, для которого я привел пример C #:

Код Гольф: число в слова

Тем не менее, его читать не так просто, и он достигает только десятичного значения. MaxValue, поэтому я написал новую версию, которая будет настолько высокой, насколько вам нужно.

Я не смог найти никакой информации о значениях выше, чем vigintillions, но если вы добавите значения в массив thou [], вы сможете продолжать подниматься, сколько захотите. Это все еще не поддерживает дроби, но я думаю о добавлении этого в некоторый момент.

    static string NumericStringToWords(string NumericValue)
    {
        if ("0" == NumericValue) return "zero";

        string[] units = { "one", "two", "three", "four", "five", 
                           "six", "seven", "eight", "nine" };

        string[] teens = { "eleven", "twelve", "thirteen", "four", "fifteen", 
                           "sixteen", "seventeen", "eighteen", "nineteen" };

        string[] tens = { "ten", "twenty", "thirty", "forty", "fifty", 
                          "sixty", "seventy", "eighty", "ninety" };

        string[] thou = { "thousand", "million", "billion", "trillion", 
                          "quadrillion", "quintillion", "sextillion", 
                          "septillion", "octillion", "nonillion", "decillion", 
                          "udecillion", "duodecillion", "tredecillion", 
                          "quattuordecillion", "quindecillion", "sexdecillion", 
                          "septendecillion", "octodecillion", "novemdecillion", 
                          "vigintillion" };

        string sign = String.Empty;
        if ("-" == NumericValue.Substring(0, 1))
        {
            sign = "minus ";
            NumericValue = NumericValue.Substring(1);
        }

        int maxLen = thou.Length * 3;
        int actLen = NumericValue.Length;
        if(actLen > maxLen)
            throw new InvalidCastException(String.Format("{0} digit number specified exceeds the maximum length of {1} digits.  To evaluate this number, you must first expand the thou[] array.", actLen, maxLen));

        //Make sure that the value passed in is indeed numeric... we parse the entire string
        //rather than just cast to a numeric type to allow us to handle large number types passed
        //in as a string.  Otherwise, we're limited to the standard data type sizes.
        int n; //We don't care about n, but int.TryParse requires it
        if (!NumericValue.All(c => int.TryParse(c.ToString(), out n)))
            throw new InvalidCastException();

        string fraction = String.Empty;
        if (NumericValue.Contains("."))
        {
            string[] split = NumericValue.Split('.');
            NumericValue = split[0];
            fraction = split[1];
        }

        StringBuilder word = new StringBuilder();
        ulong loopCount = 0;

        while (0 < NumericValue.Length)
        {
            int startPos = Math.Max(0, NumericValue.Length - 3);
            string crntBlock = NumericValue.Substring(startPos);
            if (0 < crntBlock.Length)
            {
                //Grab the hundreds tens & units for the current block
                int h = crntBlock.Length > 2 ? int.Parse(crntBlock[crntBlock.Length - 3].ToString()) : 0;
                int t = crntBlock.Length > 1 ? int.Parse(crntBlock[crntBlock.Length - 2].ToString()) : 0;
                int u = crntBlock.Length > 0 ? int.Parse(crntBlock[crntBlock.Length - 1].ToString()) : 0;

                StringBuilder thisBlock = new StringBuilder();

                if (0 < u)
                    thisBlock.Append(1 == t? teens[u - 1] : units[u - 1]);

                if (1 != t)
                {
                    if (1 < t && 0 < u) thisBlock.Insert(0, "-");
                    if (0 < t) thisBlock.Insert(0, tens[t - 1]);
                }

                if (0 < h)
                {
                    if (t > 0 | u > 0) thisBlock.Insert(0, " and ");
                    thisBlock.Insert(0, String.Format("{0} hundred", units[h - 1]));
                }

                //Check to see if we've got any data left and add
                //appropriate word separator ("and" or ",")
                bool MoreLeft = 3 < NumericValue.Length;
                if (MoreLeft && (0 == h) && (0 == loopCount))
                    thisBlock.Insert(0, " and ");
                else if (MoreLeft)
                    thisBlock.Insert(0, String.Format(" {0}, ", thou[loopCount]));

                word.Insert(0, thisBlock);
            }

            //Remove the block we just evaluated from the 
            //input string for the next loop
            NumericValue = NumericValue.Substring(0, startPos);

            loopCount++;
        }
        return word.Insert(0, sign).ToString();
    }

Я протестировал его с помощью Decimal.MaxValue, добавленного к себе, чтобы сгенерировать большое количество:

семь октоциллионов, девятьсот двадцать два септендиллиона, восемьсот шестнадцать полдекиллионов, двести пятьдесят один квиндециллион, четыреста двадцать шесть кваттоордециллионов, четыреста тридцать три третециллиона, семьсот пятьдесят девять дуодециллионов, триста пятьдесят четыре удециллиона, триста девяносто пять декалионов, тридцать три нониллиона, пять сто семьдесят девять октиллионов, двести двадцать восемь септиллионов, сто шестьдесят два секстиллиона, пятьсот четыре квинтиллиона, двести шестьдесят четыре квадриллиона, триста тридцать семь триллионов, пятьсот девяносто три миллиарда пятьсот сорок три миллиона девятьсот пятьдесят тысяч триста тридцать пять

7
задан clamp 3 June 2009 в 09:21
поделиться

1 ответ

Отредактировано для обработки фильтрации; намного сложнее: помимо получения DataRowView , нам необходимо предоставить настраиваемый компонент, который притворяется (через pass-thru PropetyDescriptor s) как DataRowView ( который сам притворяется DataRow ) - и отфильтровывает свойства, которые нам не нужны.

Очень интересная задача ;-p Легче решить в классических классах, но приведенная ниже работает для DataRow ;-p

Обратите внимание, что в этой области можно сделать другие вещи, чтобы сделать некоторые свойства недоступными для редактирования ( IsReadOnly ) или иметь другой заголовок ( DisplayName ) или категории ( Category ) - путем переопределения других элементов в RowWrapperDescriptor .

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
static class program
{
    [STAThread]
    static void Main()
    {
        DataTable table = new DataTable();
        table.Columns.Add("ID", typeof(int));
        table.Columns.Add("Foo", typeof(int));
        table.Columns.Add("Bar", typeof(string));
        table.Columns.Add("Audit", typeof(DateTime));

        table.Rows.Add(1, 14, "abc", DateTime.MinValue);
        DataRow row = table.Rows.Add(2,13,"def", DateTime.MinValue);
        table.Rows.Add(3, 24, "ghi", DateTime.MinValue);

        RowWrapper wrapper = new RowWrapper(row);
        wrapper.Exclude.Add("ID");
        wrapper.Exclude.Add("Bar");

        Application.EnableVisualStyles();
        Application.Run(new Form {Controls = {
            new PropertyGrid { Dock = DockStyle.Fill,
                SelectedObject = wrapper}}});
    }
}

[TypeConverter(typeof(RowWrapper.RowWrapperConverter))]
class RowWrapper
{
    private readonly List<string> exclude = new List<string>();
    public List<string> Exclude { get { return exclude; } }
    private readonly DataRowView rowView;
    public RowWrapper(DataRow row)
    {
        DataView view = new DataView(row.Table);
        foreach (DataRowView tmp in view)
        {
            if (tmp.Row == row)
            {
                rowView = tmp;
                break;
            }
        }
    }
    static DataRowView GetRowView(object component)
    {
        return ((RowWrapper)component).rowView;
    }
    class RowWrapperConverter : TypeConverter
    {
        public override bool GetPropertiesSupported(ITypeDescriptorContext context)
        {
            return true;
        }
        public override PropertyDescriptorCollection GetProperties(
            ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            RowWrapper rw = (RowWrapper)value;
            PropertyDescriptorCollection props = TypeDescriptor.GetProperties(
                GetRowView(value), attributes);
            List<PropertyDescriptor> result = new List<PropertyDescriptor>(props.Count);
            foreach (PropertyDescriptor prop in props)
            {
                if (rw.Exclude.Contains(prop.Name)) continue;
                result.Add(new RowWrapperDescriptor(prop));
            }
            return new PropertyDescriptorCollection(result.ToArray());
        }
    }
    class RowWrapperDescriptor : PropertyDescriptor
    {
        static Attribute[] GetAttribs(AttributeCollection value)
        {
            if (value == null) return null;
            Attribute[] result = new Attribute[value.Count];
            value.CopyTo(result, 0);
            return result;
        }
        readonly PropertyDescriptor innerProp;
        public RowWrapperDescriptor(PropertyDescriptor innerProperty)
            : base(
                innerProperty.Name, GetAttribs(innerProperty.Attributes))
        {
            this.innerProp = innerProperty;
        }


        public override bool ShouldSerializeValue(object component)
        {
            return innerProp.ShouldSerializeValue(GetRowView(component));
        }
        public override void ResetValue(object component)
        {
            innerProp.ResetValue(GetRowView(component));
        }
        public override bool CanResetValue(object component)
        {
            return innerProp.CanResetValue(GetRowView(component));
        }
        public override void SetValue(object component, object value)
        {
            innerProp.SetValue(GetRowView(component), value);
        }
        public override object GetValue(object component)
        {
            return innerProp.GetValue(GetRowView(component));
        }
        public override Type PropertyType
        {
            get { return innerProp.PropertyType; }
        }
        public override Type ComponentType
        {
            get { return typeof(RowWrapper); }
        }
        public override bool IsReadOnly
        {
            get { return innerProp.IsReadOnly; }
        }
    }
}
15
ответ дан 6 December 2019 в 14:09
поделиться
Другие вопросы по тегам:

Похожие вопросы: