Solved

Data Grid View Control, writing values to database

Posted on 2014-01-07
2
278 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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, f…

920 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

12 Experts available now in Live!

Get 1:1 Help Now