[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

Change Datgrid BackColor

Is there a way to change the text or the back color of a specifc row in a datagrid after it was already filled and binded.


datagrid1.row(6).backcolor = red
datagrid1.row(5).textforecolor = blue

Any help would be great.

  • 3
  • 2
2 Solutions
Imports System.Drawing.Drawing2D

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
            End If
        End If
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents dg As System.Windows.Forms.DataGrid
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.dg = New System.Windows.Forms.DataGrid
        CType(Me.dg, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.dg.DataMember = ""
        Me.dg.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.dg.Location = New System.Drawing.Point(0, 0)
        Me.dg.Name = "dg"
        Me.dg.Size = New System.Drawing.Size(532, 172)
        Me.dg.TabIndex = 0
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(584, 254)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.dg, System.ComponentModel.ISupportInitialize).EndInit()

    End Sub

#End Region

    Private ds As DataSet
    Private dt As DataTable

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim data_column As DataColumn

        ' Build the DataSet.
        ds = New DataSet

        ' Build the Sale table.
        dt = New DataTable("Sale")

        dt.Columns.Add("Item", GetType(String))
        dt.Columns.Add("Quantity", GetType(Integer))
        dt.Columns.Add("UnitPrice", GetType(Decimal))

        data_column = dt.Columns.Add("Total", GetType(Decimal))
        data_column.Expression = "Quantity * UnitPrice"

        ' Populate the table.
        Dim sale_data(2) As Object
        sale_data(0) = "Cookie"
        sale_data(1) = 12
        sale_data(2) = 0.25

        sale_data(0) = "Milk"
        sale_data(1) = 1
        sale_data(2) = 1.25

        sale_data(0) = "Towel"
        sale_data(1) = 4
        sale_data(2) = 5.95

        ' Attach the DataGrids to the DataTables.
        dg.DataSource = dt

        'attach table style (responcible for color)
    End Sub
    Private Sub AddCustomDataTableStyle()

        Dim ts1 As DataGridTableStyle
        ts1 = New DataGridTableStyle
        ts1.MappingName = "Sale"

        'add first column (Item)
        Dim ccItem As DataGridTextChanged.DataGridTextChangedColor
        ccItem = New DataGridTextChanged.DataGridTextChangedColor
        ccItem.MappingName = "Item"
        ccItem.HeaderText = "Item"
        ccItem.Width = 70
        AddHandler ccItem.TextBox.TextChanged, AddressOf dg_TextChanged

        'add second column(Quantity)
        Dim ccQty As DataGridTextChanged.DataGridTextChangedColor
        ccQty = New DataGridTextChanged.DataGridTextChangedColor
        ccQty.MappingName = "Quantity"
        ccQty.HeaderText = "Quantity"
        ccQty.Width = 50
        AddHandler ccQty.TextBox.TextChanged, AddressOf dg_TextChanged

        'add third column(UnitPrice)
        Dim ccPrice As DataGridTextChanged.DataGridTextChangedColor
        ccPrice = New DataGridTextChanged.DataGridTextChangedColor
        ccPrice.MappingName = "UnitPrice"
        ccPrice.HeaderText = "Price"
        ccPrice.Width = 60
        AddHandler ccPrice.TextBox.TextChanged, AddressOf dg_TextChanged

        'add calculated column(Total) - don't change color or text
        Dim c As DataGridTextBoxColumn
        c = New DataGridTextBoxColumn
        c.MappingName = "Total"
        c.HeaderText = "Total"
        c.Width = 50


        'attach tablestyle
    End Sub

    Private Sub dg_TextChanged(ByVal sender As Object, ByVal e As EventArgs)

        Dim dgcc As DataGridCell = Me.dg.CurrentCell
        Dim tb As TextBox = GetEmbeddedTextBox(dgcc.ColumnNumber)
        If Not (tb Is Nothing) Then
            Dim oldValue As String = Me.dg(dgcc).ToString()
            Dim newValue As String = tb.Text
            If newValue <> oldValue Then
                GetEmbeddedDGTextBox(dgcc.ColumnNumber).AddRow = Me.dg.CurrentRowIndex
            End If
        End If
    End Sub

    Private Function GetEmbeddedTextBox(ByVal col As Integer) As TextBox
        Dim tbc As DataGridTextBoxColumn = Me.dg.TableStyles("Sale").GridColumnStyles(col)
        If Not (tbc Is Nothing) AndAlso tbc.TextBox.Modified Then
            Return tbc.TextBox
        End If

        Return Nothing
    End Function

    Private Function GetEmbeddedDGTextBox(ByVal col As Integer) As DataGridTextChanged.DataGridTextChangedColor
        Dim tbc As DataGridTextChanged.DataGridTextChangedColor = Me.dg.TableStyles("Sale").GridColumnStyles(col)
        If Not (tbc Is Nothing) AndAlso tbc.TextBox.Modified Then
            Return tbc
        End If

        Return Nothing
    End Function

End Class

Namespace DataGridTextChanged
    Public Class DataGridTextChangedColor
        Inherits DataGridTextBoxColumn

        Public Sub New()
        End Sub

        Dim _row As New System.Collections.ArrayList

        Public Property AddRow() As Integer

            End Get
            Set(ByVal Value As Integer)
            End Set
        End Property

        Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal source As CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)

            If _row.Contains(rowNum) Then
                'color of changed cell 255, 200, 200
                backBrush = New LinearGradientBrush(bounds, Color.FromArgb(255, 200, 200), Color.FromArgb(128, 20, 20), LinearGradientMode.BackwardDiagonal)
                foreBrush = New SolidBrush(Color.White)
            End If

            MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)
        End Sub

    End Class

End Namespace

malanoisAuthor Commented:
Alrighty Then.

Now If I can follow this it would be good.

malanoisAuthor Commented:
OK, I have the class created.

I am trying to follow your code above.   I see that you are manually filling the datagrid, my grid is already filled.  

Using the above

how would i change the backcolor of row 3 in the grid.  From there i can figure out how to change the other specific rows.

Thank You,

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Check this sub
 Private Sub dg_TextChanged(ByVal sender As Object, ByVal e As EventArgs)

            If newValue <> oldValue Then
                GetEmbeddedDGTextBox(dgcc.ColumnNumber).AddRow = Me.dg.CurrentRowIndex
            End If

    End Sub

add if statement if the col# = whatever and it will work for every row but only for the specified column
malanoisAuthor Commented:
I dont want the columns in every row to change.  I want row 2 (Or whatever row number I choose) and all columns in that row to change.  It is the opposite of this code.  I think


Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now