コード例 #1
0
        public void Write(DataGridView dataGridView, int columnIndex)
        {
            List <PropertyKey> notSet = new List <PropertyKey>();

            DataGridViewGenericColumn dataGridViewGenericColumn = DataGridViewHandler.GetColumnDataGridViewGenericColumn(dataGridView, columnIndex);

            if (dataGridViewGenericColumn == null)
            {
                return;                                    //continue;
            }
            string fullFileName = dataGridViewGenericColumn.FileEntryAttribute.FileFullPath;

            using (PropertyStore propertyStore = new PropertyStore(fullFileName, PropertyStore.GetFlags.ReadWrite | PropertyStore.GetFlags.SlowItem))
            {
                for (int rowIndex = 0; rowIndex < DataGridViewHandler.GetRowCountWithoutEditRow(dataGridView); rowIndex++)
                {
                    bool isReadOnly = DataGridViewHandler.GetCellReadOnly(dataGridView, columnIndex, rowIndex);
                    DataGridViewGenericRow dataGridViewGenericRow = DataGridViewHandler.GetRowDataGridViewGenericRow(dataGridView, rowIndex);
                    object value = DataGridViewHandler.GetCellValue(dataGridView, columnIndex, rowIndex);

                    if (value is string)
                    {
                        PropertyKey propertyKey = dataGridViewGenericRow.PropertyKey;
                        string      valueString = (string)value;

                        if (!isReadOnly && propertyStore.IsEditable(propertyKey))
                        {
                            try
                            {
                                using (PropertyDescription propertyDescription = new PropertyDescription(propertyKey))
                                {
                                    if (!string.IsNullOrEmpty(valueString))
                                    {
                                        if (propertyDescription.TypeFlags.IsMultiValued)
                                        {
                                            valueString = valueString.Replace("\r\n", ";");
                                            PropVariant val = PropVariant.FromStringAsVector(valueString);
                                            propertyStore.SetValue(propertyKey, val);
                                        }
                                        else
                                        {
                                            switch (propertyDescription.PropertyType)
                                            {
                                            case VarEnum.VT_UI4:
                                                string cleanedString = new string(valueString.Where(char.IsDigit).ToArray());
                                                UInt32 valueUint32;
                                                if (UInt32.TryParse(cleanedString, out valueUint32))
                                                {
                                                    if (propertyKey.CanonicalName == "System.Rating")
                                                    {
                                                        valueUint32 = (UInt32)Metadata.ConvertRatingStarsToRatingPercent((byte)valueUint32);
                                                    }

                                                    propertyStore.SetValue(propertyKey, new PropVariant(valueUint32, propertyDescription.PropertyType));
                                                }
                                                else
                                                {
                                                    propertyStore.SetValue(propertyKey, new PropVariant());
                                                }
                                                break;

                                            case VarEnum.VT_DATE:
                                            case VarEnum.VT_FILETIME:
                                                string cleanedString2 = new string(valueString.Where(c => !char.IsControl(c)).ToArray());
                                                // https://social.msdn.microsoft.com/Forums/windows/en-US/61687b42-9882-4025-8b3a-b3251f121f94/how-to-prevent-a-textbox-to-accept-unicode-control-characters?forum=winforms
                                                cleanedString2 = Regex.Replace(cleanedString2, @"[\u200e-\u200f]", string.Empty);
                                                cleanedString2 = Regex.Replace(cleanedString2, @"[\u202a-\u202e]", string.Empty);
                                                cleanedString2 = Regex.Replace(cleanedString2, @"[\u206a-\u206f]", string.Empty);
                                                cleanedString2 = Regex.Replace(cleanedString2, @"[\u001e-\u001f]", string.Empty);

                                                DateTime valueDateTime;
                                                if (DateTime.TryParse(cleanedString2, out valueDateTime))
                                                {
                                                    propertyStore.SetValue(propertyKey, new PropVariant(valueDateTime.ToUniversalTime(), propertyDescription.PropertyType));
                                                }
                                                break;

                                            default:
                                                propertyStore.SetValue(propertyKey, new PropVariant(valueString, propertyDescription.PropertyType));
                                                break;
                                            }
                                        }
                                        //store.SetValue(key, new PropVariant(_displayProps[key], desc.PropertyType));
                                    }
                                    else
                                    {
                                        propertyStore.SetValue(propertyKey, new PropVariant());
                                    }
                                }
                                continue;
                            }
                            catch (Exception ex)
                            {
                                Logger.Error(ex);
                            }
                            notSet.Add(propertyKey);
                        }
                    }
                }
                propertyStore.Commit();
            }

            if (notSet.Count > 0)
            {
                throw new Exception("The following keys were not set:\n\t" + string.Join("\n\t", notSet.Select(k => k.CanonicalName)));
            }
        }