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,181 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…

756 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