Thread: DataGrid Value issues

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    58

    DataGrid Value issues

    I honestly cannot see why this seemingly simple click event isn't working.

    Code:
     private void button4_Click(object sender, EventArgs e)
            {
                Structs.Point2D[] PointI = new Structs.Point2D[100];
                //Structs.Point2D is just an array of two floats I created, labeled X and Y;
                for(int i = 0; i < dataGridView1.RowCount; i++) 
                {
                    PointI[i].X = (float)Convert.ToDouble(dataGridView1.Rows[i].Cells["ColumnX"].FormattedValue.ToString());
                    PointI[i].Y = (float)Convert.ToDouble(dataGridView1.Rows[i].Cells["ColumnY"].FormattedValue.ToString());
                }
                Console.WriteLine(PointI.ToString());
            }
    It says that my string input was not in the correct format...

    Any ideas?
    Last edited by arcaine01; 10-14-2008 at 07:33 PM. Reason: Didn't say what was wrong.

  2. #2
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    what doesn't work?

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    58
    Sorry...rewrote the message to include what the actual error message said haha.

    Basically all I'm trying to do is store the value of a DataGridView Cell.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    What if dataGridView1 has more than 100 rows? Use "new Structs.Point2D[dataGridView1.RowCount]" instead of "new Structs.Point2D[100]".
    You should catch the exception and watch the StackTrace property. It usually tells exactly which line (+/-1) the error lies on.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    58
    That is interesting actually...didn't know that you could dynamically change the index of an array. Awesomeness :P

    I still have the same unhandled FormatException error.



    This is the entire error in detail:
    Code:
    System.FormatException was unhandled
      Message="Input string was not in a correct format."
      Source="mscorlib"
      StackTrace:
           at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
           at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)
           at System.Double.Parse(String s, NumberStyles style, NumberFormatInfo info)
           at System.Convert.ToDouble(String value)
           at MathClass_09_30_08.Form1.button4_Click(Object sender, EventArgs e) in C:\Documents and Settings\Tyler Kendrick\My Documents\Visual Studio 2008\Projects\Math Class\MathClass_09_30_08\MathClass_09_30_08\MathClass_09_30_08\Form1.cs:line 145
           at System.Windows.Forms.Control.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ButtonBase.WndProc(Message& m)
           at System.Windows.Forms.Button.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.Run(Form mainForm)
           at MathClass_09_30_08.Program.Main() in C:\Documents and Settings\Tyler Kendrick\My Documents\Visual Studio 2008\Projects\Math Class\MathClass_09_30_08\MathClass_09_30_08\MathClass_09_30_08\Program.cs:line 19
           at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException:
    Does anyone know a way to return the value of a datagridviewcell as a float?

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    1) Make a check what type is actually contained in that cell. Something like:
    Code:
    throw new System.Exception(dataGridView1.Rows[i].Cells["ColumnX"].Value.GetType().ToString());
    (the type, prolly "float" or "string", will be shown in the exception message)

    2) Consider using "Value" instead of "FormattedValue" as it may contain more than just the value.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    58
    I used Value earlier but Value is an object and I can't find a proper cast or convert method to change an object to a float. The type of the formattedValue is just a string, but I can't seem to properly convert a string to a float either....

    Any ideas? haha

    Datagrids get me mad :P

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    object X = 3.14f;
    X is an object pointer/reference, but that doesn't mean what it points at is an object. Now go check what GetType returns, as I said

    And if value is indeed a float, just do:
    Code:
    float X = (float)Value;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    58
    Well, I found the fix thanks to you.

    Thank you for your assistance Magos!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DataGrid drag and drop
    By gtriarhos in forum C# Programming
    Replies: 0
    Last Post: 10-11-2005, 12:36 PM
  2. Date/Time in a DataGrid
    By gargamel in forum C# Programming
    Replies: 1
    Last Post: 07-29-2005, 04:52 AM
  3. Datagrid Column Header
    By gvector1 in forum Windows Programming
    Replies: 0
    Last Post: 03-15-2004, 04:08 PM
  4. DataGrid or MS Flex grid, is there one in C++ ??
    By Digs in forum Windows Programming
    Replies: 1
    Last Post: 10-30-2003, 01:43 PM
  5. Datagrid Events
    By gvector1 in forum C# Programming
    Replies: 0
    Last Post: 10-01-2003, 09:43 AM