dataGridview 索引超出范围。必须为非负值并小于集合大小。VB.NET codeDim sReturn As String获得DataGrid
dataGridview 索引超出范围。必须为非负值并小于集合大小。
VB.NET code Dim sReturn As String '获得DataGrid当前被选中的行号 Dim iRow As Integer = Me.dgdCustomerInfo.CurrentCell.RowIndex Dim sId, sCustomers, sWorker As String '获取DataGrid当前被选中的行号的供货商信息 sId = dgdCustomerInfo.SelectedRows(iRow).Cells("客户代号").Value sCustomers = dgdCustomerInfo.SelectedRows(iRow).Cells("客户全称").Value sWorker = dgdCustomerInfo.SelectedRows(iRow).Cells("业务员").Value sReturn = sId.Trim + "|" + sCustomers.Trim + "|" + sWorker.Trim + "|" Return sReturn
双击dataGridview的行要返回相关的值时就出现如下错误
未处理 System.ArgumentOutOfRangeException
Message="索引超出范围。必须为非负值并小于集合大小。
参数名: index"
ParamName="index"
Source="mscorlib"
StackTrace:
在 System.Collections.ArrayList.get_Item(Int32 index)
在 System.Windows.Forms.DataGridViewSelectedRowCollection.get_Item(Int32 index)
在 HTPS.frmCustomerInfo.SetInfo() 位置 C:\Documents and Settings\Administrator\My Documents\HTPS\HTPS\frmCustomerInfo.vb:行号 250
在 HTPS.frmOrderList.linklbSupply_LinkClicked(Object sender, LinkLabelLinkClickedEventArgs e) 位置 C:\Documents and Settings\Administrator\My Documents\HTPS\HTPS\frmOrderList.vb:行号 323
在 System.Windows.Forms.LinkLabel.OnLinkClicked(LinkLabelLinkClickedEventArgs e)
在 System.Windows.Forms.LinkLabel.OnMouseUp(MouseEventArgs e)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.Label.WndProc(Message& m)
在 System.Windows.Forms.LinkLabel.WndProc(Message& msg)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 HTPS.frmOrderList.Main() 位置 C:\Documents and Settings\Administrator\My Documents\HTPS\HTPS\frmOrderList.Designer.vb:行号 2
在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
[解决办法]VB.NET code Dim sReturn As String '获得DataGrid当前被选中的行号 Dim iRow As Integer = Me.dgdCustomerInfo.CurrentCell.RowIndex if iRow<1 then exit Sub end if Dim sId, sCustomers, sWorker As String '获取DataGrid当前被选中的行号的供货商信息 sId = dgdCustomerInfo.SelectedRows(iRow).Cells("客户代号").Value sCustomers = dgdCustomerInfo.SelectedRows(iRow).Cells("客户全称").Value sWorker = dgdCustomerInfo.SelectedRows(iRow).Cells("业务员").Value sReturn = sId.Trim + "|" + sCustomers.Trim + "|" + sWorker.Trim + "|" Return sReturn
[解决办法]
VB.NET code Dim iRow As Integer = Me.dgdCustomerInfo.CurrentCell.RowIndex if iRow<1 then '如上楼一样,这里加多一个判断,因为你点击列头或其他空白地方,rowIndex 返回的是-1 后面再调用,就会出现以上问题 exit Sub end if