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,172 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
  • 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now