ASP.NET determine number of digits after decimal point on GridView.RowDataBound - asp.net

I am binding data to my GridView control and in one of the columns I have a price column which is of type double.
I also have a key in the web.config file that tells me how many digits to display after decimal point.
I want to display the data bound field according to that key in the web.config.
I saw this post on how to set the number of digists after decimal point: DataBinding Eval To 2 Decimal Place Doesn't Show 0
but I need a way of doing that with a variable that tells how many decimal places to use.
I tried this:
Private Sub grdData_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdData.RowDataBound
Dim numOfDigitsAfterDecimal = Integer.Parse(ConfigurationManager.AppSettings("numOfDigitsAfterDecimal"))
Dim d As String = ""
For Z As Integer = 0 To numOfDigitsAfterDecimal - 1
d = d + "#"
Next
e.Row.Cells(3).Text = String.Format("{0:0.{1}}", Double.Parse(e.Row.Cells(3).Text), d)
End Sub
but it does not work, however if I do:
e.Row.Cells(3).Text = String.Format("{0:0.##}", Double.Parse(e.Row.Cells(3).Text))
it does work and displays 2 decimal places.

you can use N0 or N1 or N2 and so the number will show the decimal place. check this article
Example :
number.ToString("N3")
// N: 1,054.32
// N0: 1,054
// N1: 1,054.3
// N2: 1,054.32
// N3: 1,054.322

Related

Cannot include a cell-formula into VBA code in MS Excel

I am trying a simple VBA program, that looks like this:
Function NotWorking() As Single
If Range("C3") = 0 Then
NotWorking = "=0.6*7"
Else
NotWorking = 1.2
End If
End Function
If cell C3 has 0 as a value, the function NotWorking has to return in the current cell the result of *=0.6*7*, which is 4,2. If the value in C3 is not 0, then the current cell has to return 1,2.
In my case, things aren't the way I expected. In the current cell I type =NotWorking(). If C3 is anything but 0, I obtain 1,2, but when I appropriate 0 to C3, the current cell returns #VALUE.
Why does this code not work?
Best regards and thanks in advance!
I notice that "=0.6*7" is a string, not a number, but 1.2 is a number.
You have specified that the function returns a single, but "=0.6*7" isn't a single.
Don't try to return it from the routine, but reach out to the sheet from within the routine.
Sub NotWorking()
Dim src As Range
Dim dst As Range
Set src = Range("C3")
Set dst = Range("E3")
If src.Value = 0 Then
dst.Formula = "=0.6 * 7"
Else
dst.Value = 1.2
End If
End Sub

Finding and looping through labels

Running VS 2013 VB.net.
I have 3 labels on my aspx page labelled label1, label2 and label3.
I want to loop through each one in the code behind and assign values to them.
Here is my code
Dim X As Integer = 1
For Each obj In values
Dim myLabel As Label
myLabel = TryCast(Me.FindControl("Label" + X), Label)
myLabel.Text = Math.Round(obj, 2)
X = X + 1
Next
I know there is only 3 obj's so x will always be between 1 and 3. What am I doing wrong as I get the following.
Conversion from string "Label" to type 'Double' is not valid
IF I change ("label" + x) to ("label1") I get
Object reference not set to an instance of an object.
on the line below.
You want to concatenate strings not to calculate, in VB.NET you use & instead of +(as opposed to C#). So this should work:
myLabel = TryCast(Me.FindControl("Label" & X), Label)
You should also set Option Strict to On, without exception. Then you first have to fix a lot of compiler errors, but it will help to write much more robust code. This wouldn't compile either:
myLabel.Text = Math.Round(obj, 2)
because a Math.Round returns a Double and Label.Text is a String. You just need to use Math.Round(obj, 2).ToString() to fix it.
Here's a different approach to get your labels using Linq:
Dim myLabels = From lbl In Me.Form.Controls.OfType(Of Label)()
Where lbl.ID.StartsWith("Label")
For Each lbl As Label In myLabels
' ... '
Next
Also since your using TryCast check your variable before using it - it will be Nothing if it does not make the cast.
myLabel = TryCast(Me.FindControl("Label" & X.ToString), Label)
If myLabel IsNot Nothing Then
'safe to use variable here
End If

Select number from a string classic asp

I need to get a 4/5 digit number which always comes directly after a #
For example it'll be "Item Title (#1234)" however it's not always in the same place or at the end.
Not sure how I go about doing that
You need to manually parse the string.
I've made a quick function for this:
'//return the number after delimeter string, if exists (first occurance only)
'//in case no number exists, returns Empty value
Function FindNumberAfter(rawValue, delimeterString)
Dim index, x, curChar
Dim sBuffer
FindNumberAfter = vbEmpty
index = InStr(rawValue, delimeterString)
If index>0 Then
For x=index+Len(delimeterString) To Len(rawValue)
curChar = Mid(rawValue, x, 1)
If IsNumeric(curChar) Then
sBuffer = sBuffer & curChar
Else
Exit For
End If
Next
If Len(sBuffer)>0 Then FindNumberAfter = CLng(sBuffer)
End If
End Function
Usage in your case:
Response.Write(FindNumberAfter("Item Title (#1234)", "#"))

Array.IndexOf always returns -1

This is my code.
Public Sub SomeFucntion(ByVal test As Short)
SomeOtherFucntion(MyArray)
If Array.IndexOf(MyArray, test) <> -1
//....
End If
End Sub
test return value 10.
But i am getting IndexOf value as -1.
Inside QuickWatch in VS 2005 when i put the value as 10 instead of test i am getting the correct index.
My array is single dimensional array which now has 2,5 and 10. Since its got 10 it should ideally return 2 as the index.
The problem might be caused by the fact that your array contains integers, but the value you are looking for is a short. Consider the following example:
Dim myArray As Integer() = {5}
Dim value As Short = 5
Console.WriteLine(Array.IndexOf(myArray, value)) ' Prints -1
If the array contains integers, you need to convert your short into an integer first, for example, by using CInt:
Dim myArray As Integer() = {5}
Dim value As Short = 5
Console.WriteLine(Array.IndexOf(myArray, CInt(value))) ' Prints 0
Edit: Note that the declared type has nothing to do with this. Let's declare the array as Object, since that's what you mentioned in your comment (note that the following example requires Option Strict Off, which is bad):
Dim myArray As Object = New Integer() {5}
Dim value As Object = 5S ' Short literal
Console.WriteLine(Array.IndexOf(myArray, value)) ' still returns -1
Console.WriteLine(Array.IndexOf(myArray, CInt(value))) ' returns 0
Note: You can make that conversion implicit by declaring your function as Public Sub SomeFunction(ByVal test As Integer).
Dim test As Short = 5
Dim MyArray() As Short = {1, 3, 4, 5, 7, 3}
If Array.IndexOf(MyArray, test) <> -1 Then
MessageBox.Show("Index Found.")
End If
The above works. since test is declared as short, make sure MyArray is also declared as short.

chart datapoint operand usage

In the code snippet below I am attempting to compare two data points as I am trying to put all low value points in a pie chart dataset into on "Other" datapoint.
I am getting an "Cannot apply operator <> to operands of type System.....DataPoint and System.....DataPoint" on this code line " If pointValue <= minValue AndAlso Me.series.Points(index) <> colectedDataPoint "
Dim collectedValue As Double = 0.0
For index As Integer = 0 To Me.series.Points.Count - 1
Dim pointValue As Double = Math.Abs(Me.series.Points(index).YValues(0))
If pointValue <= minValue AndAlso Me.series.Points(index) <> colectedDataPoint Then
' Add point value to the collected value
collectedValue += pointValue
' Add point to supplemental series
Me.supplementalSeries.Points.Add(Me.series.Points(index).Clone())
' Remove point from the series
Me.series.Points.RemoveAt(index)
index -= 1
End If
Next
but yet if I write the same loop in C# that equivalent line is fine;
this.series.Points[index] != colectedDataPoint
anyone have a solution since the whole project is written in vb.net and i'd prefer to keep it uniform.

Resources