コード例 #1
0
        /// <summary>
        /// Centralize the string format settings. Does the work of checking for user
        /// overrides, and if they're not present, setting the cell alignment to match
        /// (somewhat) the source control's string alignment.
        /// </summary>
        /// <param name="alignment">String alignment</param>
        /// <param name="flags">String format flags</param>
        /// <param name="controlstyle">DataGridView style to apply (if available)</param>
        /// <param name="overrideformat">True if user overrode alignment or flags</param>
        /// <returns></returns>
        private static StringFormat managestringformat(StringAlignment alignment, StringFormatFlags flags,
                                                       DataGridViewCellStyle controlstyle, bool overrideformat)
        {
            // start with the provided
            StringFormat format = new StringFormat();

            format.Trimming    = StringTrimming.Word;
            format.Alignment   = alignment;
            format.FormatFlags = flags;

            // if the format was overridded by the user, make no adjustments
            if (!overrideformat)
            {
                // check to see if a style is provided
                if (null != controlstyle)
                {
                    // Adjust the format based on the control settings, bias towards centered
                    DataGridViewContentAlignment cellalign = controlstyle.Alignment;
                    if (cellalign.ToString().Contains("Middle"))
                    {
                        format.Alignment = StringAlignment.Center;
                    }
                    else if (cellalign.ToString().Contains("Right"))
                    {
                        format.Alignment = StringAlignment.Near;
                    }
                    else if (cellalign.ToString().Contains("Left"))
                    {
                        format.Alignment = StringAlignment.Far;
                    }
                }
            }

            return(format);
        }
コード例 #2
0
        /// <summary>
        /// Get text horizontal alignement in given cell
        /// </summary>
        /// <param name="cell"></param>
        /// <returns>text horizontal alignement in given cell</returns>
        public static StringAlignment HorizontalAlignement(this DataGridViewCell cell)
        {
            DataGridViewContentAlignment alignement = DataGridViewContentAlignment.TopLeft;

            if (cell.HasStyle && cell.Style.Alignment != DataGridViewContentAlignment.NotSet)
            {
                alignement = cell.Style.Alignment;
            }
            else
            {
                alignement = cell.InheritedStyle.Alignment;
            }

            // Check the CurrentCell alignment and apply it to the CellFormat
            if (alignement.ToString().Contains("Right"))
            {
                return(StringAlignment.Far);
            }
            else if (alignement.ToString().Contains("Center"))
            {
                return(StringAlignment.Center);
            }
            else
            {
                return(StringAlignment.Near);
            }
        }