Отключение границ одной ячейки/строки

Рейтинг: 0Ответов: 2Опубликовано: 02.10.2014

Как отключить границы одной отдельной ячейки DataGridViewCell? Вообще требуется отключить горизонтальные границы на одной строке (без разделителей между ячейками), а на остальных строках выше и ниже нужно, чтобы границы остались.

Ответы

▲ 1Принят

Нужно переопределить метод Paint для ячейки. Так работает. Хорошо работает, только если ячейка ReadOnly, а это как раз мой случай.

public class DataGridViewCustomCell : DataGridViewTextBoxCell
{
    protected override void Paint(
                 Graphics graphics,
                 Rectangle clipBounds,
                 Rectangle cellBounds,
                 int rowIndex,
                 DataGridViewElementStates cellState,
                 Object value,
                 Object formattedValue,
                 string errorText,
                 DataGridViewCellStyle cellStyle,
                 DataGridViewAdvancedBorderStyle advancedBorderStyle,
                 DataGridViewPaintParts paintParts
  )
    {

      //  base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

        SolidBrush myBrush = new SolidBrush(Color.White);

        graphics.FillRectangle(myBrush, cellBounds);
        Pen myPen = new Pen(Color.Gray, 1); //+ cellBounds.Height
        graphics.DrawLine(myPen, cellBounds.X, cellBounds.Y + cellBounds.Height- 1,
            cellBounds.X + cellBounds.Width, cellBounds.Y + cellBounds.Height -1);
        myBrush.Dispose();
        if (this.FormattedValue is String)
        {
            TextRenderer.DrawText(graphics,
                (string)this.FormattedValue,
                this.DataGridView.Font,
                cellBounds, SystemColors.GrayText);
        }

    }

}
▲ 1

dataGridView1.Rows[i].Cells[3].AdjustCellBorderStyle(.. .. ... ... ) - изменяет стиль границ ячейки в соответствии с указанными критериями. Читайте, там их много.

Выяснилось, что все не так просто. Прочитайте http://rsdn.ru/?article/dotnet/DataGridView20.xml. Есть код к статье.