0

我有 RowDatabound 事件,我想像这样更改列名:

 Protected Sub gvKeyValueOfstringstring_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvKeyValueOfstringstring.RowDataBound
        If e.Row.RowType = DataControlRowType.Header Then

            gvKeyValueOfstringstring.Columns(0).HeaderText = "Match"
            gvKeyValueOfstringstring.Columns(1).HeaderText = "Replace"
        End If
    End Sub

但我犯了错误:

指数超出范围。必须是非负数且小于集合的大小。参数名称:索引

4

2 回答 2

0

可能gvKeyValueOfstringstring.Columns长度小于 2,因此当您访问索引为 0 或 1 的列时,它会失败。

在执行该代码之前检查列是否已成功创建。

另外,我认为您不需要在 RowDataBound 事件中执行它,它可以在Page_Load(因此如果它是标题,您不必检查每一行)。如果你还是想在那里做,也许你可以改变那一行的单元格的值:

e.Row.Cells(0).Text = "Match"
e.Row.Cells(1).Text = "Replace"

无论如何,如果未正确创建列,我希望此代码也会失败。

于 2012-06-08T18:11:31.247 回答
0
Private Sub BttnCan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BttnCan.Click
        Dim val1 As Integer
        Try
            con.Open()
            For i As Integer = 0 To DataGridView1.Rows.Count
                val1 = Me.DataGridView1.Rows(i).Cells(2).Value

                com.CommandText = "INSERT INTO TblStock (Quant) VALUES (" & val1 & ")"

            Next

            con.Close()
        Catch ex As Exception
            MsgBox(Err.Description, MsgBoxStyle.Critical)
        End Try
    End Sub
于 2012-09-13T14:48:00.077 回答