Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Data Grid View Control, writing values to database

Posted on 2014-01-07
2
Medium Priority
?
288 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

971 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