vb.net 对数据网格列进行排序时出现异常“对象必须是 Double 类型”。更改值后

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13080575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 10:54:32  来源:igfitidea点击:

exception while sorting data grid column "Object must be of type Double." after changing the value

vb.net

提问by Sin

I've a data grid gridDetailswith columns discountand totalfilled from the database

我有一个gridDetails带有列的数据网格discounttotal从数据库中填充

Totalis readonly while discountis not. When the value of discount is changed, The total is recalculated as

Total是只读的,而discount不是。当折扣值发生变化时,总和重新计算为

gridDetails.Item(1, e.RowIndex).Value -= (Val(gridDetails.Item(2, e.RowIndex).Value))

after changing the value the column cant be sorted. it generates an exception

更改值后,列无法排序。它产生一个异常

System.ArgumentException was unhandled
  Message="Object must be of type Double."
  Source="mscorlib"
  StackTrace:
       at System.Double.CompareTo(Object value)
       at System.Collections.Comparer.Compare(Object a, Object b)
       at System.Windows.Forms.DataGridViewRowCollection.RowComparer.CompareObjects(Object value1, Object value2, Int32 rowIndex1, Int32 rowIndex2)
       at System.Windows.Forms.DataGridViewRowCollection.RowArrayList.Pivot(Int32 left, Int32 center, Int32 right)
       at System.Windows.Forms.DataGridViewRowCollection.RowArrayList.CustomQuickSort(Int32 left, Int32 right)
       at System.Windows.Forms.DataGridViewRowCollection.RowArrayList.CustomSort(RowComparer rowComparer)
       at System.Windows.Forms.DataGridViewRowCollection.Sort(IComparer customComparer, Boolean ascending)
       at System.Windows.Forms.DataGridView.SortInternal(IComparer comparer, DataGridViewColumn dataGridViewColumn, ListSortDirection direction)
       at System.Windows.Forms.DataGridView.Sort(DataGridViewColumn dataGridViewColumn, ListSortDirection direction)
       at System.Windows.Forms.DataGridView.OnColumnHeaderMouseClick(DataGridViewCellMouseEventArgs e)
       at System.Windows.Forms.DataGridView.OnMouseClick(MouseEventArgs e)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.DataGridView.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(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at cableguy.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       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:

i've converted the result to double using Cdbl, DirectCast etc.. no hope like gridDetails.Item(1, e.RowIndex).Value = Cdbl(gridDetails.Item(1, e.RowIndex).Value-Val(gridDetails.Item(2, e.RowIndex).Value))

我已经使用 Cdbl、DirectCast 等将结果转换为双倍。没有希望 gridDetails.Item(1, e.RowIndex).Value = Cdbl(gridDetails.Item(1, e.RowIndex).Value-Val(gridDetails.Item(2, e.RowIndex).Value))

any idea??

任何的想法??

采纳答案by Sin

what @WozzeC explained is the reason... am adding wht i've done to make it alright.. Before adding to grid i've converted the values to Double. Now its okei.. :) :happy:

@WozzeC 解释的原因是……我正在添加我所做的以使其正常……在添加到网格之前,我已将值转换为双精度值。现在它的 okei .. :) :happy:

read the data to a datareader and

将数据读取到数据读取器和

 While data.Read = True
  Dim total_amount As Double = data("discount")
  Dim discount_amount As Double = data("total")
  gridDetails.Rows.Add(total_amount ,discount_amount)
 End While

回答by WozzeC

Right, this is what I think is happening. When you load the data from the database it is displayed as strings in the table. This way it's sortable and everything is fine. Then you go and change a value with your subtraction (quite possibly the first row). This alters the sorting to become based on doubles instead. Since your entire table is displayed with strings it crashes.

是的,这就是我认为正在发生的事情。当您从数据库加载数据时,它在表中显示为字符串。这样它是可排序的,一切都很好。然后你去用你的减法改变一个值(很可能是第一行)。这将排序改为基于双打。由于您的整个表格都显示有字符串,因此它会崩溃。

So basically, eiter make really sure that you insert doubles into your Datagridview from the beginning, convert the all the values in the column after you loaded your table or insert the updated value as a string.

所以基本上,eiter 确保从一开始就将双精度插入到 Datagridview 中,在加载表后转换列中的所有值或将更新的值作为字符串插入。

回答by OldProgrammer

if you are adding doubles, decimals, etc. to a gridview and you need to add a zero where there is no value be sure to use Ctype:

如果要向 gridview 添加双精度、小数等,并且需要在没有值的地方添加零,请务必使用 Ctype:

If IsNumeric(drs.Item("qty")) then
   dgvX.row(I).cell(0).value = CType(drs.Item("qty"), decimal)
Else
   dgvX.row(I).cell(0).value = CType(0, decimal)
End If