Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I am working in VS 2008. I have a data grid view that have a column displaying numbers with decimals. A second column name Decimals. I need to set the first column's number decimals based on what is in the Decimals column. Example Number is 10.00000 and Decimals is 3. Now the Number column must b 10.000. I hope this makes sense. I hope I can do this in the data grid view's edit columns' format properties. I not then please help me with the code. Never done this before.

What I have tried:

I tried to put it in the data grid view's edit columns like N{Decimals.Value} bit it put Decimals1Value in the cell. I tried it in code but got stuck here.
For i As Integer = 0 To DataGridView1.Rows.Count

Next

I honestly don't know further. Please help!!!
Posted
Updated 22-Aug-17 21:18pm

1 solution

Try following -
VB
DataGridView1.Columns(2).DefaultCellStyle.Format = "N3"

Replace the column no. with actual column index.

More details here-
How to format Datagridview columns to numeric.?? Column.Defaultcellstyle.format does not work[^]

Hope, it helps :)
 
Share this answer
 
Comments
The Manster 23-Aug-17 3:22am    
Thanks a span! The moment I read it I recognised it from previously. Forgot about it.
Suvendu Shekhar Giri 23-Aug-17 3:27am    
Great!
The Manster 23-Aug-17 4:05am    
Sorry I am back. I thought I could implement that with some changes but you see I have to loop through the rows and set cell's format to the value of a column.
For i As Integer = 0 To DataGridView1.Rows.Count
DataGridView1.Rows(i).Cells(dgvNumber.Index).??? =
DataGridView1.Rows(i).Cells(dgvDecimals.Index).Value
Next
??? means DefaultCellStyle does not work here and I don't know what to put there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900