Issue in getting the value from datagridview cell to put into decimal - vb.net

This is my code for my button that will pop the value from the specific column.
dvList.Columns("Reg_Price").DefaultCellStyle.Format = "N2"
MsgBox(dvList.Rows(dvList.CurrentRow.Index).Cells.Item("Reg_Price").Value.ToString)
My value in the column "Reg_Price" is 0, when I click the button with the code above, the output is 0 also, I want the output to be 0.00, but the code above the msgbox is not working.
Thank you for the help.

The DefaultCellStyle.Format (and also cell format) operate only on the dataGridView display. When you get dvList.Rows(dvList.CurrentRow.Index).Cells.Item("Reg_Price").Value.ToString, you get the original cell, before formatting. You can use the vb.net Format function to format the numeric value for msgBox.

Related

Excel VBA: Get the right date into a cell using DatePicker

I have a UserForm with a DatePicker control in it.
It works fine, except when copying the selected date to the spreadsheet.
This is the code:
Range("A1").Value = UserForm1.DTPicker1.Value
Which returns:
00:00:00
In cell A1, no matter what date has been selected.
You cell formatting might be set to Time instead of Date.
Try changing that to see if it works.
Also, make formatting 'General' AFTER this step, to see if anything has been pasted in "A1".
You should need an intermediate variable to get it, like below:
t = Me.DTPicker1.Value
ws.Range("A1") = t
Make sure that the command to transfer the data is located in the same form or page of a multipage form as the DTPicker itself.
For some reason it wont work when the two are separated and will display a zero in the target cell. That zero is the "time" part of the date which has been switched off. Although the "Date" part refuses to transfer over, for some reason the time does, and so that zero time is read in the cell as time zero which is mid-day.
So in summary:
Keep the DTPicker and transfer control on the same page and that should solve the problem.
The code is pretty simple. It's
Sheet1.Range("AA9") = Me.DTPicker1.Value

#NAME? error on correct Excel formula

NAME? error is the output for the formula shown bellow. $D$6 is a number. However whenever I click inside the formula and press 'intro' as if the cell had been modified, the formula returns the correct value.
=MROUND($D$6-9.1132,1/16)
If D6 value is less than 9.1132 you have a negative number in the left part. The right part of the formula has positive number, that's why you get the error. Please ensure both parts of your formula have the same sign. Here is the man page for this.
I had the same problem. Inserted the Formula with C# and saved it.
range.Formula = "=SUMME(A1:C3)";
When I opened the workbook it would show me the "#Name?"-Error. Once klicked in the cell manually it showed the correct formula, and when i left the cell it would show the correct calculated value instead of "#Name?"
I found out that it was a localization problem. I use the german Excel, but the formulas via Excel-Com-Interop-Library have to be inserted in english!
range.Formula = "=SUM(A1:C3)";
Once i changed this, everything worked well.

Passing cell value from datagridview to textbox without clicking the cell

My program imports excel files and after this, all the data in excel has been shown in
DatagridView. I want a specific cell to pass its value to Textbox.
I have tried using Me.TextBox3.Text = DataGridView1.Rows(15).Cells(2).Value.ToString()
But there's an error which is cannot be converted in string..
row15, column2 has text..
I guess this error (error converting into string) is because the value is Nothing. The indexes begin in 0, so, as Andrey commented, for reaching row 15 and second column you have to do it this way:
Me.TextBox3.Text = DataGridView1.Rows(15).Cells(1).Value.ToString()

How to VBA change cell's value (display text) without changing its formula?

I've a problem with this VBA macro.
Sub demoChangeText()
Application.Range("B2").Value = "Overidden text"
End Sub
My test is here. To run this macro, open it in Excel, press Ctrl+F8 and choose demoChangeText.
As the result, this macro changes the value of cell B2 (the text displayed to us) but clear its formula. I need to change B2's value BUT also need the formula to be remained.
So my question is How to change the display text of cell without changing its formula?
UPDATE
I ask this question because I'm trying to solve this problem
I'm not sure if this will help, as it is a bit hard to tell what your underlying requirement is, but here goes anyway:
Several things affect the display of a cell:
the entered value, if its a constant
the result of a calculation, if its a formula
the format of the cell
the conditional format(s) of the cell, if any
In the example sheet provided you have a formula of =ROW()&COLUMN() which returns a string result of 22
You can make this display something else by applying a cell format,
eg a format of 0;0;0;Ov\e\r\ri\d\d\e\n t\ext will display any string value as Overridden text
This can by applied with VBA with
Range("B2").NumberFormat = "0;0;0;Ov\e\r\ri\d\d\e\n t\ext\s"
or
Range("B2").NumberFormat = "0;0;0;""Overridden texts"""

Getting display text as ###### for some of the cell in excel after writing from Vb.net code

I am writting to an excel file from my vb code. The code goes as below
xlsheet3 = xlBook.Sheets.Add(After:=xlSheet)
With xlsheet3
.Columns(5).NumberFormat = "#"
.Cells(j + 1, 5) = someStringValue 'Here "j" is a row counter and this line is in a "for loop"
end with
After writing to excel, most of the cells in excel are correct. But some of the cell's text comes as ####### however if I click on the cell, formula bar shows the correct result. I have tried giving single code before adding the text still that did not help.
Please help me in resolving this.
Thank you
There is not any issue with your code. You need to increase the width of the column or have to use word wrap. In excel if your value is not fully visible it shows it is "######".
If widening and wrapping text doesn't work and the format is set to text which allows display of only 255 characters, try changing the format to general.
This just indicates that the cell is too small for showing the result: make it wider.
See https://superuser.com/questions/65556/excel-displays-for-long-text-whats-wrong for some common reasons why Excel displays "######" in cells.
Either the cell is too narrow to display the contents or the contents are over 256 characters.
Check what you're writing to the cell. If it's not too long then all you need to do is resize the column to fit the new contents.
This is simply what Excel does when the data in a column is too wide to be displayed in the current column width. Make the column slightly wider and you will see all your data.
To autosize the column so it is wide enough to display all its data, double click the column divider at the right edge of the column, in the header bar.