[Webinar] Streamline your web hosting managementRegister Today

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
Suggested Courses

591 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