Solved

Looking for code to change cell color of datagrid by a change in value from a keypress

Posted on 2004-08-18
4
2,183 Views
Last Modified: 2012-08-14
In VB.Net would like to be able to visual see the cell color change (datagrid cell) when the user starts to edit the cell
Thus, showing the user that the cell has a new value. Making it easier to keep track of what cell has been modified before finishing the editing process of the total datagrid screen.
0
Comment
Question by:planocz
[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
  • 2
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 500 total points
ID: 11831350
create your datagridtextbox class something like

Option Strict Off
Option Explicit On

Imports Microsoft.VisualBasic
Imports System
Imports System.ComponentModel
Imports System.Windows.Forms

Namespace DataGridYNOnly
    Public Class DataGridYNTextBoxColumn
        Inherits DataGridTextBoxColumn
        'Fields
        'Constructors
        'Events
        'Methods
        Public Sub New()
            MyBase.New()
            AddHandler Me.TextBox.KeyPress, New System.Windows.Forms.KeyPressEventHandler(AddressOf HandleKeyPress)

        End Sub

        Private Sub HandleKeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
            Me.TextBox.BackColor = Color.Red
        End Sub
    End Class
End Namespace

call like

        Dim ts1 As DataGridTableStyle
        ts1 = New DataGridTableStyle
        ts1.MappingName = "yourtable"
        Dim csID As New DataGridYNOnly.DataGridYNTextBoxColumn
        csID.MappingName = "yourcolumn"
        csID.HeaderText = "yourtext"
        csID.Width = 60
        ts1.GridColumnStyles.Add(csID)
        DataGrid1.TableStyles.Add(ts1)
0
 
LVL 27

Author Comment

by:planocz
ID: 11831688
Hi Ron,

This is close, but I need to be able to have each cell change only if the text is changed in that cell.

Your code changes the whole column once a cell is changed.
e.i. if you move to next cell in column then that cell is red also.
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11839894
Hi planocz, I think I've found a way.

Create this custom datagridtextbox

Option Strict Off
Option Explicit On

Imports Microsoft.VisualBasic
Imports System
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Drawing.Drawing2D
Imports System.Drawing

Namespace DataGridTextChanged
    Public Class DataGridTextChangedColor
        Inherits DataGridTextBoxColumn
        'Fields
        'Constructors
        'Events
        'Methods
        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
                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


In your datagrid form use these methods

        Private Sub AddCustomDataTableStyle()

            Dim ts1 As DataGridTableStyle
            ts1 = New DataGridTableStyle
            ts1.MappingName = "Customers"
            '
            Dim csIDInt As DataGridTextChanged.DataGridTextChangedColor
            csIDInt = New DataGridTextChanged.DataGridTextChangedColor
            csIDInt.MappingName = "CustID"
            csIDInt.HeaderText = "CustID"
            csIDInt.Width = 60
            AddHandler csIDInt.TextBox.TextChanged, AddressOf dataGrid_TextChanged
            ts1.GridColumnStyles.Add(csIDInt)

            myDataGrid.TableStyles.Add(ts1)
        End Sub

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

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

        Private Function GetEmbeddedTextBox(ByVal col As Integer) As TextBox
            Dim tbc As DataGridTextBoxColumn = Me.myDataGrid.TableStyles("Customers").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.myDataGrid.TableStyles("Customers").GridColumnStyles(col)
            If Not (tbc Is Nothing) AndAlso tbc.TextBox.Modified Then
                Return tbc
            End If

            Return Nothing
        End Function



0
 
LVL 27

Author Comment

by:planocz
ID: 11840532
Ah, Yes Great minds think together.
I was thinking of using an arraylist last night to keep track of the cells.

Thanks Ron it works great.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

631 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