Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Data Grid View Control, writing values to database

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

704 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