Solved

DataGridView Validating event set cell value

Posted on 2008-10-01
7
828 Views
Last Modified: 2012-08-14
Inside the DataGridView validating event I want to set the cell's value. For example, if a user types in CA I would like to replace it with CALIFORNIA. How do you do that?

The .value description says that sets the cell value but it does not do it in the validating event.
0
Comment
Question by:DreamU
  • 3
  • 3
7 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 22622247
Can you show us what you have ?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 125 total points
ID: 22624018
You could set up the DataGridView to work in the virtual mode, and use an internal business object to show/store data.

Example:
    Private Class ShippingRecord

        Public DateSent As Date

        Public ShipToState As String

    End Class
 

    Private m_list As New List(Of ShippingRecord)
 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 

        For i As Integer = 0 To 10

            Dim rec As New ShippingRecord()

            rec.DateSent = Now.AddDays(i)

            rec.ShipToState = ""
 

            m_list.Add(rec)

        Next
 

        Me.dgv.RowCount = 10

        Me.dgv.ColumnCount = 2
 

    End Sub
 

    Private Sub DataGridView1_CellValueNeeded(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValueEventArgs) Handles dgv.CellValueNeeded
 

        If e.ColumnIndex = 0 Then

            e.Value = m_list(e.RowIndex).DateSent.ToShortDateString()

        ElseIf e.ColumnIndex = 1 Then

            e.Value = m_list(e.RowIndex).ShipToState

        End If
 

    End Sub
 

    Private Sub dgv_CellValuePushed(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValueEventArgs) Handles dgv.CellValuePushed

        If e.ColumnIndex = 1 Then

            Select Case e.Value

                Case "CA"

                    m_list(e.RowIndex).ShipToState = "CALIFORNIA"

                Case "OR"

                    m_list(e.RowIndex).ShipToState = "OREGON"

                Case "WA"

                    m_list(e.RowIndex).ShipToState = "WASHINGTON"

            End Select

        End If

    End Sub

Open in new window

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22624025
.designer code:


        Me.dgv = New System.Windows.Forms.DataGridView

        CType(Me.dgv, System.ComponentModel.ISupportInitialize).BeginInit()

        Me.SuspendLayout()

        '

        'dgv

        '

        Me.dgv.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill

        Me.dgv.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize

        Me.dgv.Location = New System.Drawing.Point(141, 116)

        Me.dgv.Name = "dgv"

        Me.dgv.RowHeadersVisible = False

        Me.dgv.Size = New System.Drawing.Size(339, 299)

        Me.dgv.TabIndex = 0

        Me.dgv.VirtualMode = True

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:DreamU
ID: 22630733
jpaulino,

In order to illustrate here is the entire program for Form1 for which the only design element on form1 is a DataGridView with 3 columns: city, state and country. There is no data source - just the grid itself.

After entering CA in the state column, the code sets its value to CALIFORNIA. The message box displays the state value when I leave the next column which is country. The state value still shows as CA in both the cell display and in the message box.


Private Sub DataGridView1_CellValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating

        Select e.ColumnIndex

            Case 1

                If e.FormattedValue = "CA" Then

                    DataGridView1(e.ColumnIndex, e.RowIndex).Value = "CALIFORNIA"

                End If

            Case 2

                MsgBox(DataGridView1("state", 0).Value)

        End Select

    End Sub

Open in new window

0
 

Author Comment

by:DreamU
ID: 22630766
TheLearnedOne:

Wow, I appreciate the effort you put into providing an alternative.

However, my actual program consists of several data grids all bound to datasources with hundreds of lines of code which is almost ready for release. This would require a large code change and I am not ready for the kind of re-testing that would be required.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22634620
Ok, good luck finding a solution.
0
 

Author Comment

by:DreamU
ID: 22763388
I need to close this item. With no direct answers I assume there is no way to set the cell's value inside a validating event.

I have foregone the functionality however TheLearnedOne provided the only work-around which may benefit others.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to get a file from SqlServer DB 4 29
Incorrect group by on LINQ query 5 27
GridDataView 6 44
Visual Studio editor? 1 16
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

706 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

18 Experts available now in Live!

Get 1:1 Help Now