?
Solved

Change Datgrid BackColor

Posted on 2005-02-24
6
Medium Priority
?
232 Views
Last Modified: 2010-04-23
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.

Example

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

Any help would be great.

malanois
0
Comment
Question by:malanois
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 28

Assisted Solution

by:iboutchkine
iboutchkine earned 1000 total points
ID: 13396002
Imports System.Drawing.Drawing2D

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        '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
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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.SuspendLayout()
        '
        'dg
        '
        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
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(584, 254)
        Me.Controls.Add(Me.dg)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.dg, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    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")
        ds.Tables.Add(dt)

        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
        dt.Rows.Add(sale_data)

        sale_data(0) = "Milk"
        sale_data(1) = 1
        sale_data(2) = 1.25
        dt.Rows.Add(sale_data)

        sale_data(0) = "Towel"
        sale_data(1) = 4
        sale_data(2) = 5.95
        dt.Rows.Add(sale_data)


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

        'attach table style (responcible for color)
        Me.AddCustomDataTableStyle()
    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
        ts1.GridColumnStyles.Add(ccItem)


        '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
        ts1.GridColumnStyles.Add(ccQty)

        '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
        ts1.GridColumnStyles.Add(ccPrice)

        '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

        ts1.GridColumnStyles.Add(c)


        'attach tablestyle
        dg.TableStyles.Add(ts1)
    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()
            MyBase.New()
        End Sub

        Dim _row As New System.Collections.ArrayList

        Public Property AddRow() As Integer
            Get

            End Get
            Set(ByVal Value As Integer)
                _row.Add(Value)
            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


0
 

Author Comment

by:malanois
ID: 13396211
Alrighty Then.

Now If I can follow this it would be good.

MJ
0
 

Author Comment

by:malanois
ID: 13396662
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,

Malanois
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

Expert Comment

by:iboutchkine
ID: 13396767
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
0
 

Author Comment

by:malanois
ID: 13403258
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

malanois
0
 
LVL 14

Accepted Solution

by:
amyhxu earned 1000 total points
ID: 13409402
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month8 days, 3 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question