索引超出范围。必须为非负值并小于集合大小。谁来帮帮我
ImportsSystem.Data.SqlClientPublicClassForm3DimconnAsNewSqlConnectionDimdsAsNewDataSe...
Imports System.Data.SqlClient
Public Class Form3
Dim conn As New SqlConnection
Dim ds As New DataSet
Public flag As Integer
Function open() As Long
ds.Tables.Clear()
Dim str = "select * from 采购单"
Dim da As New SqlDataAdapter(str, conn)
da.Fill(ds)
DataGridView1.ColumnCount = ds.Tables(0).Columns.Count
DataGridView1.RowCount = ds.Tables(0).Rows.Count
For i = 0 To ds.Tables(0).Rows.Count - 1
For j = 0 To ds.Tables(0).Columns.Count - 1
DataGridView1.Rows(i).Cells(j).Value = ds.Tables(0).Rows(i).ItemArray(j)
Next
Next
For i = 0 To ds.Tables(0).Columns.Count - 1
DataGridView1.Columns(i).HeaderText = ds.Tables(0).Columns(i).ColumnName
Next
End Function
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conn.ConnectionString = "server=.;integrated security=sspi;database=class"
conn.Open()
Call open()
Dim a = "select * from 采购明细 where 采购单号= '" & DataGridView1.SelectedRows(0).Cells(0).Value & "'" 展开
Public Class Form3
Dim conn As New SqlConnection
Dim ds As New DataSet
Public flag As Integer
Function open() As Long
ds.Tables.Clear()
Dim str = "select * from 采购单"
Dim da As New SqlDataAdapter(str, conn)
da.Fill(ds)
DataGridView1.ColumnCount = ds.Tables(0).Columns.Count
DataGridView1.RowCount = ds.Tables(0).Rows.Count
For i = 0 To ds.Tables(0).Rows.Count - 1
For j = 0 To ds.Tables(0).Columns.Count - 1
DataGridView1.Rows(i).Cells(j).Value = ds.Tables(0).Rows(i).ItemArray(j)
Next
Next
For i = 0 To ds.Tables(0).Columns.Count - 1
DataGridView1.Columns(i).HeaderText = ds.Tables(0).Columns(i).ColumnName
Next
End Function
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conn.ConnectionString = "server=.;integrated security=sspi;database=class"
conn.Open()
Call open()
Dim a = "select * from 采购明细 where 采购单号= '" & DataGridView1.SelectedRows(0).Cells(0).Value & "'" 展开
展开全部
报错在DataGridView1.SelectedRows(0).Cells(0),这里需要检查SelectedRows总数是否大于0,再要判断DataGridView1.SelectedRows(0).Cells总数是否大于0,如果索引器参数超过集合总数自然会报错
追问
我今天又看了下代码,发现不是代码的问题,是datagridview控件属性的问题,要把第一个datagridview的selectionmode改成fullrowselect
你答错了不给分,给你的知识。。
追答
答错了?哪里答错了,你这本来就是索引超出范围的问题,设置datagridview的目的还是为了解决索引超出范围的问题,自己没搞清楚就瞎说
索引超出范围是异常,然后根据异常去找根本问题,在不同的上下文下造成这异常的原因也不一样,你又没提供足够的信息,你只说selectionmode我却得考虑多种原因,病人得病的原因只有一种,但医生在第一时间根据症状得出的原因却可能多种,所以才有望闻问切的多种手段,这种问问题的基本常识都不懂
你可以说我的回答不足以解决你的问题,但我的回答并没有错,太没诚意了,是非不是你一张嘴说了算的,做技术居然有妄下结论的毛病,不可教也
参考资料: http://msdn.microsoft.com/zh-cn/library/system.argumentoutofrangeexception.aspx
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询