Solved

Data Grid View Control, writing values to database

Posted on 2014-01-07
2
276 Views
Last Modified: 2014-01-07
I have a datagridviewcontrol, and I have this code that works to force certain values to upper case:

    Private Sub dgvMLK_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles dgvMKL.CellFormatting
        If e.ColumnIndex = 0 Or e.ColumnIndex = 2 Or e.ColumnIndex = 3 Or e.ColumnIndex = 4 Or e.ColumnIndex = 6 Or e.ColumnIndex = 8 Or e.ColumnIndex = 10 Then
            If e.Value <> Nothing Then
                e.Value = e.Value.ToString().ToUpper()
                e.FormattingApplied = True
            End If
        End If
    End Sub

At the row validated event, I want to commit this data to the database and I use this technique:

    Private Sub dgvMLK_RowValidated(sender As Object, e As DataGridViewCellEventArgs) Handles dgvMKL.RowValidated
        'when we are working on what WAS the last row,
        'it has now become the second to the last row
        'if that's the row being validated, then we want to write the records, including the audit table
        'TODO:  working here
        Try
            If e.RowIndex = Me.dgvMKL.Rows(Me.dgvMKL.Rows.Count - 2).Index Then
                Dim dr As DataGridViewRow = Me.dgvMKL.Rows(Me.dgvMKL.Rows.Count - 2)

                Dim dc As dcKittingdc = New dcKittingdc(Me._ConnString)

                Dim newMKL As MKL = New MKL

                'TODO: are we doing any validation here
                newMKL.Job_Number = dr.Cells("colJob").Value.ToString()
                newMKL.SS_Number = Convert.ToDouble(dr.Cells("colSS").Value.ToString())
                newMKL.Top_Collector = dr.Cells("colTopCollector").Value.ToString()
                newMKL.Install = dr.Cells("colInstall").Value.ToString()
                newMKL.Our_Part_Number = dr.Cells("colMacPart").Value.ToString().ToUpper()
                newMKL.Real_Part_Number = dr.Cells("colRealPart").Value.ToString().ToUpper()
                newMKL.Qty = Convert.ToDouble(dr.Cells("colQty").Value.ToString())
                newMKL.uom = dr.Cells("colUOM").Value.ToString()
                newMKL.Active = Convert.ToBoolean(dr.Cells("colActive").Value)
                newMKL.User = dr.Cells("colUser").Value.ToString()
                newMKL.DateTime = Convert.ToDateTime(dr.Cells("colDateTime").Value.ToString())

                'Note, the rest of the fields are null
                dc.MKLs.InsertOnSubmit(newMKL)

                Dim newAud As MKLAudit = New MKLAudit

                newAud.Job_number = dr.Cells("colJob").Value.ToString()
                newAud.SS_Number = Convert.ToDouble(dr.Cells("colSS").Value.ToString())
                newAud.Top_Collector = dr.Cells("colTopCollector").Value.ToString()
                newAud.Install = dr.Cells("colInstall").Value.ToString()
                newAud.Our_Part_Number = dr.Cells("colMacPart").Value.ToString().ToUpper()
                newAud.Real_Part_Number = dr.Cells("colRealPart").Value.ToString().ToUpper()
                newAud.Qty = Convert.ToDouble(dr.Cells("colQty").Value.ToString())
                newAud.uom = dr.Cells("colUOM").Value.ToString()
                newAud.Active = Convert.ToBoolean(dr.Cells("colActive").Value)
                newAud.User = dr.Cells("colUser").Value.ToString()
                newAud.DateTime = Convert.ToDateTime(dr.Cells("colDateTime").Value.ToString())
                newAud.aud_action = "A"
                newAud.aud_dt = New DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day)
                newAud.aud_tm = New DateTime(1900, 1, 1, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second)
                newAud.aud_user = Me._User

                dc.MKLAudits.InsertOnSubmit(newAud)

                dc.SubmitChanges()
            End If

        Catch ex As Exception
            Globals.WriteMessage(Me, "E", ex.Message, "frmEditMKL/dgvMLK_RowValidated")
        End Try


OurPartNumber and RealPartNumber are two of the fields being forced to upper case.  Yet, when they are written to the database they are lower case, unless I use the .ToUpper() as you see in the code.  I wouldn't expect to have to use that.

What am I doing wrong?


Thanks,
0
Comment
Question by:g_johnson
  • 2
2 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 39762735
CellFormatting handles how the data is displayed, but the data keeps its own format underneath.

I have a few DataGridViews that have columns that hold Double values, but display them as Fraction (5 3/16). Very useful, because fractions are not interesting to me as a programmer to perform calculations, but they are the way that the woodworkers that use the application want to see the data.
0
 
LVL 40
ID: 39762753
By the way, instead of forcing the display of uppercase in the CellFormatting event, do it in the CellValueChanged event. That way any input done by the user will be forced to uppercase.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

705 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

13 Experts available now in Live!

Get 1:1 Help Now