James Richards

James Richards

  • NA
  • 6
  • 9.8k

Currency formating on unbound datagridview textbox Winforms

Dec 16 2011 11:22 AM
This is my problem, I have a datagridview on form1 from there I move the values of a single row to a second form with datagridview. Both datagridviews are unbound and i the first all columns are added with code, on the second form only 4 of the columns are added this way one is a amount column. The first grid the first few columns are populated using sql and then the rest are comboboxes populated by sql or text. when I move the values of a single row to the second form and have the amount field formatted to currency all looks good. Then I add a row copying the fields I need for the next record from the first, the amount is not one of the fields copied. When the currency is entered it does not format but stays as just a number. If I change the original row it also just stays a number.
The purpose of this program is that when checks come in they may pay more than one statement. the second screen is so the check can be split and records updated. I am under a strict deadline and this is my first Visual C# .net (using visual studio 2010 sql server 2008) I am a mainframe programmer and needing to make the change but I have been searching for a solution for this for two days and have not found it yet, so please help if you can. 
This is the code from the creation of the amount column
 DataGridViewTextBoxColumn colAmount = new DataGridViewTextBoxColumn();
            colAmount.HeaderText = "Amount";
            dgvSplitCheck.Columns.Insert(4, colAmount);
            dgvSplitCheck.Columns[4].DefaultCellStyle.Format = "c";
There is code to give a total to match and make sure the amounts total to the check
private void dgvSplitCheck_CellEndEdit(object sender, DataGridViewCellEventArgs e)
       {
           if (dgvSplitCheck.CurrentCell.ColumnIndex.Equals(4))
           {
               int sum = 0;
               for (int i = 0; i < dgvSplitCheck.Rows.Count; ++i)
               {
                   sum += Convert.ToInt32(dgvSplitCheck.Rows[i].Cells[4].Value);
                 
               }
               lblTotal.Text = sum.ToString();
              
               dgvSplitCheck.CurrentRow.Cells[4].Style.Format = "C2";
              


I added the last line to see if that would fix my problem it didnt.

Thank you in advance, Jim

Answers (4)