none
The operands '-' and '/' are not operands of type 'string' and 'int'. RRS feed

  • Question

  • The operands '-' and '/' are not operands of type 'string' and 'int'. How do I solve an error?

    My value is in string format.

      private void MoveRow(int sourceRow, int targetRow)
            {
                if (sourceRow == targetRow)
                    return;
                GridView view = grvProgramStdInfo;
    
                DataRow row0 = null;
                DataRow row1 = null;
                DataRow row2 = null;
    
                string val1 = "";
                string val2 = "";
    
                if (targetRow == sourceRow + 1)
                {
                    row1 = view.GetDataRow(sourceRow);
                    row2 = view.GetDataRow(targetRow);
                    val1 = (string)row1[OrderFieldName];
                    val2 = (string)row2[OrderFieldName];
                    row1[OrderFieldName] = val2;
                    row2[OrderFieldName] = val1;
    
                    view.FocusedRowHandle = sourceRow + 1;
                    return;
                }
    
                if (sourceRow == targetRow + 1)
                {
                    row1 = view.GetDataRow(sourceRow);
                    row2 = view.GetDataRow(targetRow);
                    val1 = (string)row1[OrderFieldName];
                    val2 = (string)row2[OrderFieldName];
                    row1[OrderFieldName] = val2;
                    row2[OrderFieldName] = val1;
    
                    view.FocusedRowHandle = sourceRow - 1;
                    return;
                }
    
    
                row0 = view.GetDataRow(targetRow - 1);
                row1 = view.GetDataRow(targetRow);
                row2 = view.GetDataRow(targetRow + 1);
                DataRow dragRow = view.GetDataRow(sourceRow);
                val1 = (string)row1[OrderFieldName];
                if (row2 == null)
                    dragRow[OrderFieldName] = val1 + 1;
                else
                {
                    val2 = (string)row2[OrderFieldName];
                    if (row0 == null)
                        dragRow[OrderFieldName] = val1 - 1;
                    else
                        dragRow[OrderFieldName] = (val1 + val2) / 2;
                }
            }



    Friday, July 19, 2019 12:16 AM

All replies

  • Greetings Chh552.

    The most failsafe way to convert strings to numbers is with TryParse. Another possibility is Convert, if you are certain the string is numeric and don't need to check.

             string numberString = "99";
             int numberInt = 0;
    
             // Use TryParse to check the string is really numeric.
             if (int.TryParse(numberString, out numberInt))
             {
                numberInt += 5;
                Console.WriteLine(numberInt.ToString());
             }
    
             // Or use Convert if you do not need to check.
             int newInt = Convert.ToInt32(numberString);


    Friday, July 19, 2019 1:58 AM
  • Hi chh.552,

    Thank you for posting here.

    Based on your description, you want to solve the error that you got.

    You could try the following modified code.

                 if (row2 == null)
                    dragRow[OrderFieldName] = int.Parse(val1) + 1;
                    else
                   {
                    val2 = (string)row2[OrderFieldName];
                    if (row0 == null)
                        dragRow[OrderFieldName] = int.Parse(val1) - 1;
                    else
                        dragRow[OrderFieldName] = (int.Parse(val1) + int.Parse(val2) )/ 2;
                   }

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 19, 2019 6:49 AM
    Moderator