Conditional formatting of DataGridView cell data - Change color on negative - vb.net

I was hoping to be able to use color based conditional formatting in the DefaultCellStyle.Format field for DataGridView cells, in a similar way to how Excel handles this.
For example in Excel, a format string of £#,##0.00;[Red]-£#,##0.00 will display negative values in red.
Is this supported in VB.NET ?
I am aware I can use the .CellFormatting event to conditionally change cell text color but was looking for a less bulky and restrictive way of doing this.

By creating the following CellFormatting addition, I am able to use Excel style conditional colour formatting in the cells format field. Setting the colour for negative/positive/zero values is supported.
Format string is expected to be in the following format (all colours optional) :
[colour]<format for +value> ; [colour]<format for -value> ; [colour]<format for zero value>
..a test DGV column with conditional formatting
c = New DataGridViewColumn
c.Name = "AmountOUT"
c.DataPropertyName = c.Name
c.HeaderText = "AmountOUT"
c.CellTemplate = New DataGridViewTextBoxCell
c.DefaultCellStyle.Format = "[Green]£0.00;[Red]-£0.00;[Blue]zero"
.Columns.Add(c)
..
Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
'Split format string to positive / negative / zero components
Dim posnegzero As List(Of String)
posnegzero = e.CellStyle.Format.Split(CChar(";")).ToList
Dim coloursPNZ As New List(Of String)
Dim remainderformatPNZ As String = ""
For Each s As String In posnegzero
If s.Contains("[") And s.Contains("]") Then
'Extract [xxx] contents
coloursPNZ.Add(s.Substring(s.IndexOf("[") + 1, s.IndexOf("]") - s.IndexOf("[") - 1))
'Append rebuilt format excluding [xxx]
remainderformatPNZ &= s.Substring(0, s.IndexOf("[")) & s.Substring(s.IndexOf("]") + 1, s.Length - s.IndexOf("]") - 1) & ";"
Else
coloursPNZ.Add("")
remainderformatPNZ &= s & ";"
End If
Next
'Set format excluding any [xxx] components
e.CellStyle.Format = remainderformatPNZ
'Check for positive value
If Val(e.Value) > 0 And coloursPNZ.Count >= 1 Then
If coloursPNZ(0) <> "" Then
e.CellStyle.ForeColor = Color.FromName(coloursPNZ(0))
End If
End If
'Check for negative value
If Val(e.Value) < 0 And coloursPNZ.Count >= 2 Then
If coloursPNZ(1) <> "" Then
e.CellStyle.ForeColor = Color.FromName(coloursPNZ(1))
End If
End If
'Check for zero value
If Val(e.Value) = 0 And coloursPNZ.Count >= 3 Then
If coloursPNZ(2) <> "" Then
e.CellStyle.ForeColor = Color.FromName(coloursPNZ(2))
End If
End If
End Sub

Dim dgv As DataGridView = Me.DataGridView1
For i As Integer = 0 To dgv.Rows.Count - 1
For ColNo As Integer = 4 To 7 ' number columns
If Not dgv.Rows(i).Cells(ColNo).Value < 0 Then
dgv.Rows(i).Cells(ColNo).Style.BackColor = vbcolor.Red
End If
Next
Next
checking for negative values lookout for strings format and check accordingly
Tryparse will convert the input to an integer if it succeeds - you don't need both the comps and value variables. Here's an example of how it works:
Dim comps As Integer
Dim input As String = "im not an integer"
Dim input2 As String = "2"
'tryparse fails, doesn't get into comps < 0 comparison
If Integer.TryParse(input, comps) Then
If comps < 0 Then
'do something
End If
Else
'I'm not an integer!
End If
'tryparse works, goes into comps < 0 comparison
If Integer.TryParse(input2, comps) Then
If comps < 0 Then
'do something
End If
End If

Related

prevent go to next row when duplicated value in `datagridview`

I have datagrid that user will add values in just one column , and i want to prevent duplicated data in this column, i had mange that by the code bellow,
what I want is to keep the selection (focus) in the same editing cell(x)
if the entered data is duplicated, so I tried to get the current row index and return to it if the data is duplicated but its not working.
row_index = DataGridView1.CurrentRow.Index.ToString()
Dim cell As DataGridViewCell = DataGridView1.Rows(row_index).Cells(1)
DataGridView1.CurrentCell = cell
DataGridView1.BeginEdit(True)
NOTE : User will Add barcode number, so he will use barcode scanner or add it manually and press enter key.
Private Sub DataGridView1_RowValidated(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.RowValidated
If DataGridView1.Rows.Count > 2 Then
Dim i As Integer = 0
Dim row_index As Integer
' loop condition will loop while the row count is less or equal to i
While i <= DataGridView1.Rows.Count - 1
Dim j As Integer = 1
' loop condition will loop while the row count is less or equal to j
While j <= DataGridView1.Rows.Count - 1
Dim str As String = DataGridView1.Rows(i).Cells(1).Value()
Dim str1 As String = DataGridView1.Rows(j).Cells(1).Value()
If Not str1 = "" AndAlso Not str = "" Then
If str1 = str Then
'row_index = DataGridView1.SelectedCells.Item(i).RowIndex.ToString()
row_index = DataGridView1.CurrentRow.Index.ToString()
Dim cell As DataGridViewCell = DataGridView1.Rows(row_index).Cells(1)
DataGridView1.CurrentCell = cell
DataGridView1.BeginEdit(True)
Exit Sub
End If
End If
j += 1
End While
i += 1
End While
End If
End Sub
You can try it also in a for loop. This is how I do it. If what you mean is to stop/retain the selection(focus) or go to in a cell/row whenever it is a duplicate with the current data you have. This should work fine.
enter code here
For i = 0 To Datagridview1.Rows.Count - 1
If Datagridview1.Rows(i).Cells(1).Value = DataYouWillInput Then
DataGridView1.CurrentCell = DataGridView1.Rows(i).Cells(1)
Exit for
End If
Next
enter code here
If your condition returns true with the condition of current data and data you'll be adding. Just exit your loop. Use this.
DataGridView1.CurrentCell = DataGridView1.Rows(i).Cells(1)

Export DataGridView to text file keeping columns lined up

I am working on project for school, using VB, I am working in Visual Studio 2017.
I have a DataGridView which I need to export to a Text File.
I could use some help with an export feature from VB to a Text file. Here is the code I am using:
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim numCols As Integer = dgvApplianceList.ColumnCount
Dim numRows As Integer = dgvApplianceList.RowCount - 1
Dim strDestinationFile As String = "exportappliance.txt"
Dim tw As TextWriter = New StreamWriter(strDestinationFile)
'writing the header
For count As Integer = 0 To numCols - 1
tw.Write(dgvApplianceList.Columns(count).HeaderText)
If (count <> numCols - 1) Then
tw.Write(vbTab)
End If
Next
tw.WriteLine()
For count As Integer = 0 To numRows - 1
For count2 As Integer = 0 To numCols - 1
tw.Write(dgvApplianceList.Rows(count).Cells(count2).Value)
If (count2 <> numCols) Then
tw.Write(vbTab)
End If
Next
tw.WriteLine()
Next
tw.Close()
End Sub
Since you are writing to a “text” file, one way to line up text properly can be accomplished using spaces as others have suggested. This would require that you have a “defined” column “width” for each column. Using your picture as an example, column 0 (zero) would be “Appliance Type” and we could give that column a max “width” of… say twenty five (25) characters wide. Column 2 “kwh” could be set with a maximum column width of 15 and so on for each column.
With the “column widths” established, it should be a simple matter of adding X number of spaces needed to fill the string to the columns width. Example, to make sure column 2 lines up with the next column 2, each column 1 string MUST be all the same length. By filling each column 1 string with spaces to “fill” the string to column 1’s length, will ensure column 2’s text will line up correctly. Obviously, the same logic applies to subsequent columns.
The GetBufferedString method (below) demonstrates one way to buffer the strings to a specified column width. The method takes a string originalString, an int maxLength and a justification type. The method will return a string of length maxLength such that, if the justification type is LEFT, the method will fill the given string with spaces at the end. If the justification type is RIGHT, the method will return a string of maxLength such that spaces are added to the front of the string. Finally, if the justification type is CENTER, then the method will return a string with half the spaces in front of the string and the other half at the end. If the given string’s length is greater than maxLength, then the returned string will be a maxLength truncation of the given string.
This should enable you to set each columns justification type independently. The code below simply sets each rows justification type to right.
This is an example and I hope it helps, however there is no error checking for a possible mismatch on the number of actual columns in the grid and the number of column widths.
Some global variables… an integer array columnLengths is used to hold each columns width… also an enumeration for the justification type; RIGHT, LEFT, CENTER.
Dim columnLengths(6) As Integer
Enum JustifyType
LEFT
RIGHT
CENTER
End Enum
Set each columns width…
Private Sub FillColumnLength()
columnLengths(0) = 25
columnLengths(1) = 12
columnLengths(2) = 12
columnLengths(3) = 12
columnLengths(4) = 12
columnLengths(5) = 12
End Sub
An updated save button click event to use the GetBufferedString method.
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim numCols As Integer = dgvApplianceList.ColumnCount
Dim numRows As Integer = dgvApplianceList.RowCount - 1
Dim strDestinationFile As String = "D:\Test\exportappliance.txt"
Dim tw As TextWriter = New StreamWriter(strDestinationFile)
Dim textToOutput = ""
For count As Integer = 0 To numCols - 1
textToOutput = GetBufferedString(dgvApplianceList.Columns(count).HeaderText, columnLengths(count), JustifyType.CENTER)
tw.Write(textToOutput)
Next
tw.WriteLine()
For count As Integer = 0 To numRows - 1
For count2 As Integer = 0 To numCols - 1
textToOutput = GetBufferedString(dgvApplianceList.Rows(count).Cells(count2).Value, columnLengths(count2), JustifyType.RIGHT)
tw.Write(textToOutput)
Next
tw.WriteLine()
Next
tw.Close()
End Sub
Finally, the GetBufferedString method.
Private Function GetBufferedString(originalString As String, maxLength As Int16, justifyType As JustifyType) As String
If (originalString.Length < maxLength) Then
Dim bufString = Space(maxLength - originalString.Length)
Select Case justifyType
Case JustifyType.LEFT
Return originalString + bufString
Case JustifyType.RIGHT
Return bufString + originalString
Case JustifyType.CENTER
Dim halfString = bufString.Substring(bufString.Length / 2)
originalString = halfString + originalString
bufString = Space(maxLength - originalString.Length)
Return originalString + bufString
Case Else
Return ""
End Select
Else
Return originalString.Substring(0, maxLength)
End If
End Function
Hope this helps.

How do you check if an input is a negative number in VB

I am trying to do some validation which checks if the value in a textbox is an integer then checks if the the value is negative. It correctly checks if the value is an integer but I can't get it to check if the value is negative.
Note: The value being entered is the number of competitions attended so comps = competition etc...
Dim comps As Integer
Dim value As Double
If Integer.TryParse(txtCompsEntered.Text, integer) Then
value = txtCompsEntered.Text
If value < 0 Then
lblcompsatten.ForeColor = Color.Red
txtCompsEntered.ForeColor = Color.Red
lblcompsatten.Text = "No negative numbers"
Else
lblcompsatten.ForeColor = Color.Black
txtCompsEntered.ForeColor = Color.Black
lblcompsatten.Text = ""
End If
lblcompsatten.ForeColor = Color.Black
txtCompsEntered.ForeColor = Color.Black
lblcompsatten.Text = ""
Else
lblcompsatten.ForeColor = Color.Red
txtCompsEntered.ForeColor = Color.Red
lblcompsatten.Text = "Not a number"
End If
I have already looked at this thread but it didn't seem to work
how-to-check-for-negative-values-in-text-box-in-vb
Tryparse will convert the input to an integer if it succeeds - you don't need both the comps and value variables. Here's an example of how it works:
Dim comps As Integer
Dim input As String = "im not an integer"
Dim input2 As String = "2"
'tryparse fails, doesn't get into comps < 0 comparison
If Integer.TryParse(input, comps) Then
If comps < 0 Then
'do something
End If
Else
'I'm not an integer!
End If
'tryparse works, goes into comps < 0 comparison
If Integer.TryParse(input2, comps) Then
If comps < 0 Then
'do something
End If
End If
There are a couple of things off with your code but the main issue is using Integer.TryParse incorrectly.
Incorrect:
Dim value As Double
If Integer.TryParse(txtCompsEntered.Text, integer) Then
value = txtCompsEntered.Text
If value < 0 Then
Correct:
Dim value As Integer
If Integer.TryParse(txtCompsEntered.Text, value) Then
If value < 0 Then
The things to note are that Integer.TryParse will return a boolean value (true if the value can be convertan integer, false if not). It will them dump the converted value into the second parameter you pass into it. In your case, you had 'integer', which is incorrect. You should be passing in a variable and then using that variable for your comparison.
Also, be careful with your types. You have 'value' as a double when you seem to be working with integers.
Maybe try this?
If myinteger.toString.Contains("-") Then
'it's negative
Else
'it isn't
End If
Or even simplier
If myinteger < 0 Then
'it's not negative
Else
'it is negative
End if

Counting blank text box as 0 value While Text Boxes are Empty

I am have written the following code:
Dim i As Integer
Dim pos As Integer = 0
Dim neg As Integer = 0
Dim zer As Integer = 0
Dim TextBoxes() As String = {Val(TextBox1.Text), Val(TextBox2.Text),
Val(TextBox3.Text), Val(TextBox4.Text),
Val(TextBox5.Text), Val(TextBox6.Text),
Val(TextBox7.Text), Val(TextBox8.Text),
Val(TextBox9.Text), Val(TextBox10.Text)}
For i = 0 To 9
If TextBoxes(i) > 0 Then
pos += 1
End If
If TextBoxes(i) < 0 Then
neg += 1
End If
If TextBoxes(i) = 0 Then
zer += 1
End If
Next i
Label4.Text = (pos)
Label5.Text = (neg)
Label6.Text = (zer)
When the program executes and I put some values into the text boxes, the output looks like this. The first text box contains 1 which is positive and the other one contains -1 which is negative. It's working well.
The problem occurs here: the program is counting the empty boxes as 0 and displaying 8 in the total number of zeros. All of the other 8 text boxes were left blank. How can I Fix the issue so that it doesn't count the empty text boxes as 0.
For reference, here is my related, previous problem which has already been solved: Finding String of Substring in VB without using library function
The problem is that you are calling the Val function to get the value in each text box. Val returns 0 if the given text is empty or non-numeric. If you want to check that, you should just store the original strings in the array and then check the value in the loop, like this:
Dim i As Integer
Dim pos As Integer = 0
Dim neg As Integer = 0
Dim zer As Integer = 0
Dim TextBoxes() As String = {TextBox1.Text, TextBox2.Text,
TextBox3.Text, TextBox4.Text,
TextBox5.Text, TextBox6.Text,
TextBox7.Text, TextBox8.Text,
TextBox9.Text, TextBox10.Text}
For i = 0 To 9
If TextBoxes(i) <> String.Empty Then
If Val(TextBoxes(i)) > 0 Then
pos += 1
End If
If Val(TextBoxes(i)) < 0 Then
neg += 1
End If
If Val(TextBoxes(i)) = 0 Then
zer += 1
End If
End If
Next i
Label4.Text = pos.ToString()
Label5.Text = neg.ToString()
Label6.Text = zer.ToString()
However, the Val function is mainly just provided for backwards compatibility with VB6. It will work, but I would recommend using Integer.TryParse instead. Note that I also added ToString to the last three lines. As others have mentioned, it would behoove you to turn Option Strict On.

Prevent decimal truncation in Word 2003

I have a document template which is auto populated via an external web service. The incoming data exists as a currency (e.g. 3.10) but when it is passed into the Word Document template the variable is truncated to remove any trailing 0's. I need the number to always appear with 2 decimals, even if they are both 0's.
This is with the 2003 version of Word, I have not tested with other versions since all of our document templates need to be generated using that version of Word.
You should be able to utilize the Format function in a macro to do this:
Format(yourValue, "Currency")
To have a user entered text box that can only accept currency formatted values, I've used macros like this:
Private Function getValue(text As String) As Currency
If text = "" Then
getValue = 0
Else
getValue = CCur(Val(RemoveNonNumeric(text)))
End If
End Function
Private Function RemoveNonNumeric(inputStr As String) As String
Const NUMERIC_CHARS = "0123456789."
Dim result As String
Dim currCharIndex As Long
Dim currentString As String
Dim deciCount As Integer
Dim afterDeciCount As Integer
deciCount = 0
afterDeciCount = 0
For currCharIndex = 1 To Len(inputStr)
currentString = Mid$(inputStr, currCharIndex, 1)
If currentString = "." Then deciCount = deciCount + 1
If InStr(1, NUMERIC_CHARS, currentString) > 0 And deciCount < 2 And afterDeciCount < 3 Then
result = result + currentString
If deciCount > 0 Then afterDeciCount = afterDeciCount + 1
End If
Next
result = result
RemoveNonNumeric = result
End Function