Exemplo n.º 1
0
        protected override void OnRender(Graphics paint, Rectangle cellRect, string cellValue, CellStyleInfo style, DataColumnBase column, RowColumnIndex rowColumnIndex)
        {
            int       starHeight, starWidth;
            Rectangle drawArea;

            var padding = 5;

            starWidth  = 16;
            starHeight = 16;

            var RadioButtonColumn = column.GridColumn as GridRadioButtonColumn;

            drawArea = new Rectangle(cellRect.X + padding, cellRect.Y + ((cellRect.Height / 2) - (starHeight / 2)), starWidth, starHeight);

            RadioButtonCollection = new List <GridCellRadioButton>();
            for (int i = 0; i < RadioButtonColumn.ItemCount; i++)
            {
                var radioButton = new GridCellRadioButton();

                radioButton.Location = new Point(drawArea.X, drawArea.Y);
                radioButton.Width    = starWidth;
                radioButton.Height   = starHeight;

                //Draw outer border of RadioButton
                radioButton.DrawBorder(paint, Color.Black, drawArea, radioButton);

                Point point = new Point(drawArea.X + drawArea.Width + 2, drawArea.Y);
                Font  font  = style.GetFont() != Control.DefaultFont ? style.GetFont() : Control.DefaultFont;

                int value       = i;
                var buttonValue = ((Options)value).ToString();

                //Draw string for RadioButton
                paint.DrawString(buttonValue, font, new SolidBrush(Color.Gray), point);
                if (buttonValue.Equals(cellValue))
                {
                    radioButton.DrawSelectedCheckMark(paint, drawArea, radioButton, Color.Black, Color.Black);
                }
                //Add RadioButton to Cell
                RadioButtonCollection.Add(radioButton);

                //Set the start point for next RadioButton
                Size stringlenth = TextRenderer.MeasureText((RadioOptions = 0).ToString(), font);
                drawArea.X += drawArea.Width + 10 + stringlenth.Width;
            }
        }
Exemplo n.º 2
0
        protected override void HighlightSearchText(Graphics paint, DataColumnBase column, CellStyleInfo style,
                                                    Rectangle bounds, string cellValue, RowColumnIndex rowColumnIndex)
        {
            if (AllowHighlightSearchText && !string.IsNullOrEmpty(SearchText))
            {
                if (column != null && (SearchColumns.Count > 0 && !SearchColumns.Contains(column.GridColumn.MappingName)))
                {
                    return;
                }

                var      searchText     = SearchText;
                string[] metaCharacters = { "\\", "^", "$", "{", "}", "[", "]", "(", ")", ".", "*", "+", "?", "|", "<", ">", "-", "&" };
                if (metaCharacters.Any(searchText.Contains))
                {
                    for (int i = 0; i < metaCharacters.Length; i++)
                    {
                        if (searchText.Contains(metaCharacters[i]))
                        {
                            searchText = searchText.Replace(metaCharacters[i], @"\" + metaCharacters[i]);
                        }
                    }
                }

                searchText = "^(" + searchText + ")$";

                //Gets the list of indexes that match with the search text.
                List <int> matchList = Regex.Matches(cellValue, searchText, this.AllowCaseSensitiveSearch ? RegexOptions.None : RegexOptions.IgnoreCase)
                                       .Cast <Match>()
                                       .Select(s => s.Index).ToList();



                if (matchList.Count > 0 && bounds.Width > 0)
                {
                    StringFormat format = new StringFormat(StringFormat.GenericDefault);

                    List <CharacterRange[]> characterRangeList = new List <CharacterRange[]>();

                    //Used 32 as CharacterRangeLength to avoid System.OverFlowExcepion
                    //in StringFormat.SetMeasurableCharacterRanges which occurs if the CharacterRange length is more than 32.
                    int characterRangeLength = 32;
                    int count      = matchList.Count / characterRangeLength;
                    int range      = matchList.Count % characterRangeLength;
                    int startIndex = 0;

                    if (count > 0)
                    {
                        for (int index = 0; index < count; index++)
                        {
                            CharacterRange[] characterRange = new CharacterRange[characterRangeLength];

                            for (int i = 0; i < characterRangeLength; i++)
                            {
                                characterRange[i] = new CharacterRange(matchList[startIndex], SearchText.Length);
                                startIndex++;
                            }

                            characterRangeList.Add(characterRange);
                            characterRange = null;
                        }
                    }

                    if (range > 0)
                    {
                        CharacterRange[] characterRange = new CharacterRange[range];
                        for (int i = 0; i < range; i++)
                        {
                            characterRange[i] = new CharacterRange(matchList[startIndex], SearchText.Length);
                            startIndex++;
                        }

                        characterRangeList.Add(characterRange);
                        characterRange = null;
                    }

                    //VerticalAlignment
                    format.LineAlignment = ConvertToStringAlignment(style.VerticalAlignment);

                    //HorizontalAlignment
                    format.Alignment = ConvertToStringAlignment(style.HorizontalAlignment);

                    if (!column.GridColumn.AllowTextWrapping)
                    {
                        format.FormatFlags = StringFormatFlags.NoWrap;
                        format.Trimming    = column.GridColumn.TextTrimming;
                    }

                    if (DataGrid.RightToLeft == RightToLeft.Yes)
                    {
                        format.FormatFlags = StringFormatFlags.DirectionRightToLeft;
                    }

                    foreach (var characterRange in characterRangeList)
                    {
                        //Set the range of characters to be measured.
                        format.SetMeasurableCharacterRanges(characterRange);

                        //Gets the regions of the measurable characters applied to the string format for the given text with in the text bounds.
                        Region[] regions = paint.MeasureCharacterRanges(cellValue, style.GetFont(), bounds, format);

                        RectangleF[] fillRectangle = new RectangleF[regions.Length];
                        for (int i = 0; i < regions.Length; i++)
                        {
                            fillRectangle[i] = regions[i].GetBounds(paint);
                        }

                        var highlightColor = SearchColor;
                        if (this.CurrentRowColumnIndex == rowColumnIndex)
                        {
                            highlightColor = SearchHighlightColor;
                        }

                        // Highlights the search text based on the
                        using (Brush brush = new SolidBrush(highlightColor))
                            paint.FillRectangles(brush, fillRectangle);
                    }

                    characterRangeList.Clear();
                    characterRangeList = null;
                    matchList.Clear();
                    matchList = null;
                }
            }
        }