Solved

Data Grid View Control, writing values to database

Posted on 2014-01-07
2
282 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET - Refactor Class per SOLID principles 2 43
Get sourcecode path 14 62
vb.net datagrid scroll automatically to the first found row 11 46
Need help with a query 14 54
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 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