1

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

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

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

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

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:

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

任何的想法??

4

4 回答 4

2

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

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

于 2012-10-26T06:49:25.133 回答
2

如果要向 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
于 2016-09-20T18:06:25.577 回答
0

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

将数据读取到数据读取器并

 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
于 2012-10-26T07:16:55.817 回答
0
            foreach(DataGridViewRow rowV in gridVerifyedData.Rows)
            {
                rowV.Cells["Tot_Work_hours"].Value = Convert.ToDouble(rowV.Cells["Tot_Work_hours"].Value);
            }
                gridVerifyedData.Sort(gridVerifyedData.Columns["Tot_Work_hours"], ListSortDirection.Descending);
于 2020-11-16T06:20:47.707 回答