DGV not formatting decimals

bones

Well-known member
Joined
Aug 23, 2014
Messages
143
Programming Experience
Beginner
I had my DGV working exactly the way I wanted it to. Now it's broken and I can't figure out what's wrong.
My form load has this and it was working great....
VB.NET:
[COLOR=green]'format DGV1 formulated colums for proper number type[/COLOR]
       [COLOR=blue]Me[/COLOR].DGV1.Columns.Item(4).DefaultCellStyle.Format = [COLOR=#a31515]"n1"[/COLOR] 
       [COLOR=blue]Me[/COLOR].DGV1.Columns.Item(5).DefaultCellStyle.Format = [COLOR=#a31515]"n3"[/COLOR] 
       [COLOR=blue]Me[/COLOR].DGV1.Columns.Item(6).DefaultCellStyle.Format = [COLOR=#a31515]"n3"[/COLOR] 
       [COLOR=blue]Me[/COLOR].DGV1.Columns.Item(7).DefaultCellStyle.Format = [COLOR=#a31515]"n3"[/COLOR] 
       [COLOR=blue]Me[/COLOR].DGV1.Columns.Item(8).DefaultCellStyle.Format = [COLOR=#a31515]"n1"[/COLOR] 
       [COLOR=blue]Me[/COLOR].DGV1.Columns.Item(9).DefaultCellStyle.Format = [COLOR=#a31515]"n1"[/COLOR]
 
       [COLOR=blue]Me[/COLOR].DGV1.AllowUserToResizeColumns = [COLOR=blue]True[/COLOR]
       [COLOR=blue]Me[/COLOR].DGV1.AllowUserToOrderColumns = [COLOR=blue]False[/COLOR]
       [COLOR=blue]Me[/COLOR].DGV1.MultiSelect = [COLOR=blue]True[/COLOR]
 
       [COLOR=green]'decimal formatting because the dgv source is a datatable generated by Sub "DataGridViewToDataTable"[/COLOR]
       [COLOR=blue]Me[/COLOR].DGV1.Columns(0).ValueType = [COLOR=blue]GetType[/COLOR]([COLOR=blue]Double[/COLOR])
       [COLOR=blue]Me[/COLOR].DGV1.Columns(4).ValueType = [COLOR=blue]GetType[/COLOR]([COLOR=blue]Double[/COLOR])
       [COLOR=blue]Me[/COLOR].DGV1.Columns(5).ValueType = [COLOR=blue]GetType[/COLOR]([COLOR=blue]Double[/COLOR])
       [COLOR=blue]Me[/COLOR].DGV1.Columns(6).ValueType = [COLOR=blue]GetType[/COLOR]([COLOR=blue]Double[/COLOR])
       [COLOR=blue]Me[/COLOR].DGV1.Columns(7).ValueType = [COLOR=blue]GetType[/COLOR]([COLOR=blue]Double[/COLOR])
       [COLOR=blue]Me[/COLOR].DGV1.Columns(8).ValueType = [COLOR=blue]GetType[/COLOR]([COLOR=blue]Double[/COLOR])
       [COLOR=blue]Me[/COLOR].DGV1.Columns(9).ValueType = [COLOR=blue]GetType[/COLOR]([COLOR=blue]Double[/COLOR])


First, I'm confused as with cdbl, val, substring as far as which to use, when. I read val will return a string as a double and cdbl will return an expression as a double. I've also seen substring x,x used to get decimal values..

I'll probably need to reveal more code before this is resolved but could someone please tell me if I have val an cdbl used correctly here>


VB.NET:
[COLOR=blue]Private[/COLOR] [COLOR=blue]Sub[/COLOR] TBoxInThroatDiam_TextChanged([COLOR=blue]ByVal[/COLOR] sender [COLOR=blue]As[/COLOR] System.[COLOR=#2b91af]Object[/COLOR], [COLOR=blue]ByVal[/COLOR] e [COLOR=blue]As[/COLOR] System.[COLOR=#2b91af]EventArgs[/COLOR]) [COLOR=blue]Handles[/COLOR] TBoxInThroatDiam.TextChanged
        [COLOR=green]'For Changes made to Intake Throat Diameter[/COLOR]
 
        [COLOR=blue]Dim[/COLOR] incount = Val([COLOR=blue]Me[/COLOR].TBoxInValveCount.Text)  [COLOR=green]' Quantity of valves[/COLOR]
        [COLOR=blue]Dim[/COLOR] inthroatdiam = Val([COLOR=blue]Me[/COLOR].TBoxInThroatDiam.Text) [COLOR=green]' Throat Diameter[/COLOR]
        [COLOR=blue]Dim[/COLOR] inthroatarea = [COLOR=blue]CDbl[/COLOR](inthroatdiam * inthroatdiam * 0.7854)
 
        [COLOR=blue]If[/COLOR] TBoxInValveDiam.Text = ([COLOR=#a31515]""[/COLOR]) [COLOR=blue]Then[/COLOR]
            [COLOR=blue]Exit Sub[/COLOR]
        [COLOR=blue]Else[/COLOR]
            [COLOR=blue]For[/COLOR] [COLOR=blue]Each[/COLOR] r [COLOR=blue]As[/COLOR] [COLOR=#2b91af]DataGridViewRow[/COLOR] [COLOR=blue]In[/COLOR] [COLOR=blue]Me[/COLOR].DGV1.Rows
                [COLOR=blue]If[/COLOR] r.Cells(11).Value = [COLOR=#a31515]"Intake"[/COLOR] [COLOR=blue]Then[/COLOR]
                    [COLOR=green]'Populates Calculated Throat Velocity using formula CTV = (CFM / throat area) * 2.14 / count[/COLOR]
                    r.Cells(8).Value = (Val(r.Cells(2).Value) / inthroatarea) * 2.14 / incount
                [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
            [COLOR=blue]Next[/COLOR]
        [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
    [COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]
 
First, I'm confused as with cdbl, val, substring as far as which to use, when. I read val will return a string as a double and cdbl will return an expression as a double. I've also seen substring x,x used to get decimal values.

You should pretty much never use Val. Val will create a Double from a String by starting to read characters from the beginning of the String and stopping when it gets to a character that would cause the result to not be a valid number. The problem with that is that it might produce a number that the user never intended. For instance, if the user meant to enter "25076" but accidentally entered "25o76" then Val will return the number 25.0 and that is clearly not what the user intended. The one time I would suggest that Val is useful is when you have a String that you know starts with a number and you want to extract that number, e.g. an address.

CDbl will convert a value to a Double if a valid conversion exists. While it can be used to convert Strings, Decimals, etc, I would suggest only using it to cast actual Double values that are stored as Object references, e.g. the Value of a DataGridViewCell or the SelectedValue of a ComboBox.

Substring is used to extract any part of a String. If you want to extract part of a String for any purpose, use Substring.

If you want to extract part of a String and convert it to a Double then use String.Substring to extract the substring and then Convert.ToDouble, Double.Parse or perhaps Double.TryParse to convert it to a Double.
 
You should pretty much never use Val. Val will create a Double from a String by starting to read characters from the beginning of the String and stopping when it gets to a character that would cause the result to not be a valid number. The problem with that is that it might produce a number that the user never intended. For instance, if the user meant to enter "25076" but accidentally entered "25o76" then Val will return the number 25.0 and that is clearly not what the user intended. The one time I would suggest that Val is useful is when you have a String that you know starts with a number and you want to extract that number, e.g. an address.

CDbl will convert a value to a Double if a valid conversion exists. While it can be used to convert Strings, Decimals, etc, I would suggest only using it to cast actual Double values that are stored as Object references, e.g. the Value of a DataGridViewCell or the SelectedValue of a ComboBox.

Substring is used to extract any part of a String. If you want to extract part of a String for any purpose, use Substring.

If you want to extract part of a String and convert it to a Double then use String.Substring to extract the substring and then Convert.ToDouble, Double.Parse or perhaps Double.TryParse to convert it to a Double.

Thank you.

With regard to the situation I'm trying to resolve, that being the DGV columns no formatting correctly anymore [not correct number of decimal places] I need to ask, when it comes to a DGV using formulas as mine does, what makes the final determination of the desired decimal places?

Does the form load code that specifically formats the default cell style have the final say or do the declarations in the formulas over ride that formatting? Something broke on me when I added some code to determine the formulas applied to the cells based on the value of column11 and I can't figure out what/why.
 

Latest posts

Back
Top