我有这样一段程序:用datagrid 绑定一个表其中左后一列为 添加
If e.CommandName = "add" Then   
            Dim cpi As Integer = dgclient.CurrentPageIndex    '取得当前页索引
            Dim i As Integer = e.Item.ItemIndex                '取得当前页行数
            Dim row As Integer = cpi * 3 + i                    '总行数
            Dim id As Integer = dgclient.Items(row).Cells(0).Text'取得当前选的行的第一列值
            Dim str As String = "select count(*) from vclient1 where myid='"& id &"'"
            Dim bcom As New SqlCommand(str, myconn1.conn)
当我在第二页点击“添加"出发事件时就出现错误
索引超出范围。必须为非负值并小于集合大小。参数名: index
我在第一页点击“添加“时可以执行没有错误,我在断点调试时,第二页点“添加“后id 怎么为0 到底是哪儿出现了错误?请大家帮忙,谢谢!

解决方案 »

  1.   

    在 第2页添加 之前先把页索引返回。
    DataGrid1.CurrentPage=0,试试。
      

  2.   

    Dim id As Integer = dgclient.Items(row).Cells(0).Controls(0).Text这样看看
      

  3.   

    不行 text 不是controls成员函数
      

  4.   

    答ty58801283(小新)
     这难道不是asp.net 技术论坛!
      

  5.   

    If e.CommandName = "add" Then   
                Dim cpi As Integer = dgclient.CurrentPageIndex    '取得当前页索引
                Dim i As Integer = e.Item.ItemIndex                '取得当前页行数
                Dim row As Integer = cpi * 3 + i                    '总行数
                Dim id As Integer = dgclient.Items(row).Cells(0).Text'取得当前选的行的第一列值
                Dim str As String = "select count(*) from vclient1 where myid='"& id &"'"改成If e.CommandName = "add" Then   
                
                Dim myid As Integer = DataGrid1.DataKeys(e.Item.ItemIndex)
                Dim str As String = "select count(*) from vclient1 where myid="& myid