-
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?
-
-
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.
-
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.
-
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?
-
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.
-
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
-
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;
-
Well, I found the fix thanks to you. :)
Thank you for your assistance Magos!