Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Percentage entered as whole number but converted to decimal
 

Percentage entered as whole number but converted to decimal

I have a databound datagridview that has a percentage column using this format on the column: #0.##%. currently, the cell requires you to type the percentage as a decimal (.20 for 20%). It is then saved to the database as a decimal. I want the user to type 20 and have it save to the database as .20. How can I accomplish this? I considered writing some code to take what was writtan and multiply it by .01 on the cellendedit event. this might work, but is there an easier way to accomplish this?
MethodMas  Friday, August 28, 2009 9:04 PM

Using cellendedit method is recommended. As an alternatively, you can handle cellformatting event. Check whether the cell value is bigger than 1, if so, multiplyby 0.01. There are many things need to be considered. The following code is not completed but can give a hint. You need to add more logic to get and set the value.

void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)

{

if (e.ColumnIndex == 0)

{

if (e.Value != null && e.Value.ToString()!=""&& double.Parse(e.Value.ToString()) > 1 )

{

e.Value = double.Parse(e.Value.ToString()) * 0.01;

}

e.CellStyle.Format = "p";

}

}

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Friday, September 04, 2009 10:34 AM
I guess..... Nope !



Thanks

â™?My Blog â™?My Flickr â™?My Facebook â™?/div>
Omie  Saturday, August 29, 2009 6:15 PM

Using cellendedit method is recommended. As an alternatively, you can handle cellformatting event. Check whether the cell value is bigger than 1, if so, multiplyby 0.01. There are many things need to be considered. The following code is not completed but can give a hint. You need to add more logic to get and set the value.

void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)

{

if (e.ColumnIndex == 0)

{

if (e.Value != null && e.Value.ToString()!=""&& double.Parse(e.Value.ToString()) > 1 )

{

e.Value = double.Parse(e.Value.ToString()) * 0.01;

}

e.CellStyle.Format = "p";

}

}

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Friday, September 04, 2009 10:34 AM

You can use google to search for other answers

Custom Search

More Threads

• Finding values in a databound combobox
• how to update connection string???
• Yet another .addNew question
• Using DataRowCollection Can't Insert New Rows Between Row and Row. Please Help !!!
• Read a value of Datgrid cell...
• How to Remove DataGrid's Default Column
• PrintPreviewDialog and printing don't use the sorted DataView Please Help!
• Project Publish Error
• copying datagridview rows within the datagridview
• How to programatically move active cell in DataGridview?