我有一个数据网格gridDetails
,数据库中填充了discount
和total
列
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等将结果转换为double ..没有希望
喜欢
gridDetails.Item(1, e.RowIndex).Value = Cdbl(gridDetails.Item(1, e.RowIndex).Value-Val(gridDetails.Item(2, e.RowIndex).Value))
任何想法?
答案 0 :(得分:2)
是的,这就是我认为正在发生的事情。 从数据库加载数据时,它在表中显示为字符串。这种方式可以排序,一切都很好。然后你去减去你的减法值(很可能是第一行)。这改变了基于双打的排序。由于整个表都显示有字符串,因此会崩溃。
所以基本上,eiter确保你从一开始就将双打插入到你的Datagridview中,在你加载表之后转换列中的所有值或者将更新后的值作为字符串插入。
答案 1 :(得分:0)
将数据读取到datareader并
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
答案 2 :(得分:0)
如果要向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
答案 3 :(得分: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);