Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Базы данных под .NET > Выделение цветом строки в гриде


Автор: Fyrklod 19.7.2007, 09:04
Как можно во всем грде выделить ячейку, а лучше строку другим цветом, чтобы эти данные выделялись на основе других? К примеру... есть данные : старые запчасти(красный), запчасти на гарантии(зеленный)... ну вот хотелось бы их увидеть на фоне остальных выделеными... можно ли так сделать?

Автор: SergeOK 3.10.2007, 17:24
на VB.Net примерно так

--------------------
на уровне класса 

dim DataGridViewCellStyle1 As System.Windows.Forms.DataGridViewCellStyle = New System.Windows.Forms.DataGridViewCellStyle
    Dim DataGridViewCellStyle2 As System.Windows.Forms.DataGridViewCellStyle = New System.Windows.Forms.DataGridViewCellStyle


sub new()

...
  DataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft
        DataGridViewCellStyle1.BackColor = System.Drawing.Color.WHITE
        DataGridViewCellStyle1.Font = New System.Drawing.Font("Arial", 9.0!, System.Drawing.FontStyle.Bold)
        DataGridViewCellStyle1.ForeColor = System.Drawing.Color.Black
        DataGridViewCellStyle1.Padding = New System.Windows.Forms.Padding(3, 0, 3, 0)
        DataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Yellow
         DataGridViewCellStyle1.SelectionForeColor = System.Drawing.Color.black
        DataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.False

DataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft
        DataGridViewCellStyle2.BackColor = System.Drawing.Color.BLUE
        DataGridViewCellStyle2.Font = New System.Drawing.Font("Arial", 9.0!, System.Drawing.FontStyle.Regular)
        DataGridViewCellStyle2.ForeColor = System.Drawing.Color.Black
        DataGridViewCellStyle2.Padding = New System.Windows.Forms.Padding(3, 0, 3, 0)
        DataGridViewCellStyle2.NullValue = "-"
        DataGridViewCellStyle2.SelectionBackColor = System.Drawing.Color.Navy
        DataGridViewCellStyle2.SelectionForeColor = System.Drawing.Color.white
        DataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.False
...

' грид не привязан к источнику данных 

'RDS - это датасет откуда грузятся записи

     For i = 0 To RDS.Tables(0).Rows.Count - 1
             Dim r1 As New DataGridViewRow
             r1.CreateCells(Me.DataGridView1)
                               
                            If RDS.Tables(0).Rows(i).Item(1)  Then / например в первой колонке статус старая                               
                                     r1.DefaultCellStyle = DataGridViewCellStyle1
                             elseIf RDS.Tables(0).Rows(i).Item(2)  Then / например во второй колонке статус гарантия
                                     r1.DefaultCellStyle = DataGridViewCellStyle2
                              else
                                // будет применен стиль по умолчанию
                             End If
                         r1.CreateCells(Me.DataGridView1, RDS.Tables(0).Rows(i).ItemArray)
                         Me.DataGridView1.Rows.Add(r1)
               r1=nothing
      next

Автор: CYBERDREAM 4.10.2007, 18:14
Выделить цветом можно (если 2005 студия так) так 
Код

            dataGridView1.Rows[index].DefaultCellStyle.BackColor = Color.Red;


Осталось подписаться на нужное событие и проверять нужную ячейку, отлавливать номер строки и пихать вместо в качестве номера строки

Автор: CYBERDREAM 4.10.2007, 21:49
http://www.gotdotnet.ru/DotNet/FAQ/WindowsForms/DataGrid/305.aspx

Так же хороший способ!

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)