Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-08-18
4
Medium Priority
?
2,185 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

664 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