0

I want to check values in grid columns when performing multiplication between Quantity and Price values. I have done this and it works perfectly!

But I have a problem when I enter a string value in column Quantity or after I edit the column and leave it empty. I am getting an error:

enter image description here

So how can I check the values and when the user types string, char or blank input, show a MessageBox with feedback?

Here is my code:

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            int price, quantity, total;         

             quantity= int.Parse(dataGridView1.Rows[e.RowIndex].Cells["Quantity"].Value.ToString());
             cena = int.Parse(dataGridView1.Rows[e.RowIndex].Cells["Price"].Value.ToString());
             total = quantity * price ;

            dataGridView1.Rows[e.RowIndex].Cells["Total"].Value = total;
        }

When I enter an integer everything works OK...

Gordon Mackie JoanMiro
  • 3,499
  • 3
  • 34
  • 42
Ivan
  • 5,139
  • 11
  • 53
  • 86
  • 2
    If your data was in a datatable you could use an expression column to automatically multiply the total. Otherwise this is a dupe – Ňɏssa Pøngjǣrdenlarp Sep 02 '16 at 20:43
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp Sep 02 '16 at 20:43
  • I know what is that exception. I need like i say how to check what type of value is entered in cell – Ivan Sep 02 '16 at 20:51
  • If you just added some columns and did not set the datatype, they will all be string. use `TryParse` for cases where they enter letters or spaces (or force the type to some numerics) – Ňɏssa Pøngjǣrdenlarp Sep 02 '16 at 21:02
  • Forget about `CellEndEdit` or all other events. Just use computed column in your `DataTable`. [`DataColumn.Expression`](https://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression(v=vs.110).aspx) is what you are looking for. – Reza Aghaei Sep 03 '16 at 11:38
  • my cumns is not in `DataTable` becouse i manually create grid `dataGridView1.Columns[3].Name = "quatity"`.. etc – Ivan Sep 03 '16 at 13:14
  • 1
    So create a `DataTable` first! Don't use `DataGridView` without data source. – Reza Aghaei Sep 03 '16 at 13:45
  • Ok i will create few tests with `DataTable` and will check – Ivan Sep 03 '16 at 13:48
  • 1
    Take a look at this post [How can I merge 2 data properties and show them in a single Column of DataGridView?](http://stackoverflow.com/a/39140273/3110834) – Reza Aghaei Sep 03 '16 at 13:57
  • @Ivan Let me know if you have any question about linked post. – Reza Aghaei Sep 04 '16 at 21:04
  • Ah sorry forgot for this. I slove problem woth `DataTable` – Ivan Sep 04 '16 at 21:19
  • Good job, you used the best solution :) – Reza Aghaei Sep 04 '16 at 22:59

2 Answers2

0

Try this, and assign some value if the input is not able to convert to int.

    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
            {
                int price, quantity, total;         

   if(dataGridView1.Rows[e.RowIndex].Cells["Quantity"].Value != null)
    {
        if(!int.TryParse(dataGridView1.Rows[e.RowIndex].Cells["Quantity"].Value.ToString(), out quantity))
            {
                 quantity= 0;
            }
    }else
    {
        quantity= 0;
    }    
                             price = int.Parse(dataGridView1.Rows[e.RowIndex].Cells["Price"].Value.ToString());

                     total = quantity * price ;

                    dataGridView1.Rows[e.RowIndex].Cells["Total"].Value = total;
                }
Vijai
  • 2,369
  • 3
  • 25
  • 32
0

Use the try... catch block code

        void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e){ 

           int price, quantity, total;         
           try {
                 quantity= int.Parse(dataGridView1.Rows[e.RowIndex].Cells["Quantity"].Value.ToString());
                 cena = int.Parse(dataGridView1.Rows[e.RowIndex].Cells["Price"].Value.ToString());
                 total = quantity * price ;

                 dataGridView1.Rows[e.RowIndex].Cells["Total"].Value = total;
           } catch(NullReferenceException)  { ... }
       } 

You can do nullable type

int? price, quantity, total;

And read here

https://msdn.microsoft.com/en-us/library/b3h1hf19(v=vs.110).aspx

Jack1987
  • 727
  • 1
  • 14
  • 26
  • This slove problem when col is blank but not slove when i enter `char` or `string`. When i enter char or string i get `An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll` `Additional information: Input string was not in a correct format.` – Ivan Sep 02 '16 at 22:21
  • The System.FormatException is because your code is not converting the string to integer, Int32.Parse(...) – Jack1987 Sep 06 '16 at 13:57