【问题标题】:Adding new row to unbound datagridview将新行添加到未绑定的 datagridview
【发布时间】:2019-05-23 14:54:23
【问题描述】:

我有一个未绑定的数据网格视图。由于我对网格中的数据做的各种事情,我不想绑定它。这些列是在 datagridview 的设置(编辑列)中预定义的。 我想创建一个新行,然后用数据填充网格行。我正在尝试使用 .Add.Rows 方法,但它失败了

{"索引超出范围。必须为非负数且小于集合的大小。" & vbCrLf & "参数名称:索引"}

以下 SQL 检索数据:

USE CCAP
declare @ScheduleName as varchar(30) = 'Walk-In Center April Wk 1 2019'
Select ShiftName, ScheduleStart, ScheduleEnd, Position, ADP_ID1,
       Name1,ADP_ID2, Name2, ADP_ID3, Name3, ADP_ID4, Name4, ADP_ID5,
       Name5, ADP_ID6, Name6, ADP_ID7, Name7 
from FormattedSchedules 
where ScheduleName = @ScheduleName;

并且行数大于 0,因此它正在获得结果。 我不明白什么索引超出范围或为什么集合为 0 代码如下:

试过.Rows.Add(1).Rows.Add().Rows.Add("")

    Dim FSchedCmd As SqlCommand
    Dim FSchedSQL As String
    Dim FSchedConn As New SqlConnection()
    Dim FSchedadapter As New SqlDataAdapter()
    Dim i As Integer = 0
    Dim rowIndex As Integer
    Dim row As DataGridViewRow

    AddedNewRow = 1

    Dim dsFSched As New DataSet()
    FSchedSQL = "Select ShiftName, ScheduleStart, ScheduleEnd, Position, ADP_ID1, Name1, ADP_ID2, Name2, ADP_ID3, Name3, ADP_ID4, Name4, ADP_ID5, Name5, ADP_ID6, Name6, ADP_ID7, Name7 from FormattedSchedules where ScheduleName = @ScheduleName;"
    Try
        If GlobalVariables.logProd = 1 Then
            GlobalVariables.strConnection = "CCAPProdConnectionString"
        Else
            GlobalVariables.strConnection = "CCAPTestConnectionString"
        End If
        FSchedConn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings(GlobalVariables.strConnection).ConnectionString
        FSchedConn.Open()
        FSchedCmd = New SqlCommand(FSchedSQL, FSchedConn)
        FSchedCmd.Parameters.Add("@ScheduleName", SqlDbType.VarChar).Value = cboCreateScheduleName.Text
        FSchedadapter.SelectCommand = FSchedCmd
        FSchedadapter.Fill(dsFSched)
        FSchedadapter.Dispose()
        FSchedCmd.Dispose()
        FSchedConn.Close()
        'dgvCreateSchedule.DataSource = dsFSched.Tables(0)
        dgvCreateSchedule.Rows.Clear()
        With dgvCreateSchedule
            Dim RowNo As Long = 0
            '.RowCount = 0
            While RowNo <= dsFSched.Tables(0).Rows.Count - 1

                .Rows.Add(1)
                .Rows(RowNo).Cells(0).Value = dsFSched.Tables(0).Rows(RowNo).Item(0) 'ShiftName
                '.Rows(RowNo).Cells(1).Value = dsFSched.Tables(0).Rows(RowNo).Item(1) 'Start Time
                .Rows(RowNo).Cells(1).Value = Convert.ToDateTime(dsFSched.Tables(0).Rows(RowNo).Item(1)).TimeOfDay
                '.Rows(RowNo).Cells(2).Value = dsFSched.Tables(0).Rows(RowNo).Item(2) 'End Time
                .Rows(RowNo).Cells(2).Value = Convert.ToDateTime(dsFSched.Tables(0).Rows(RowNo).Item(2)).TimeOfDay 'End Time
                .Rows(RowNo).Cells(3).Value = dsFSched.Tables(0).Rows(RowNo).Item(3) 'Position
                .Rows(RowNo).Cells(4).Value = dsFSched.Tables(0).Rows(RowNo).Item(4) 'ADP_ID1
                .Rows(RowNo).Cells(5).Value = dsFSched.Tables(0).Rows(RowNo).Item(5) 'Name1
                .Rows(RowNo).Cells(6).Value = dsFSched.Tables(0).Rows(RowNo).Item(6) 'ADP_ID2
                .Rows(RowNo).Cells(7).Value = dsFSched.Tables(0).Rows(RowNo).Item(7) 'Name2
                .Rows(RowNo).Cells(8).Value = dsFSched.Tables(0).Rows(RowNo).Item(8) 'ADP_ID3
                .Rows(RowNo).Cells(9).Value = dsFSched.Tables(0).Rows(RowNo).Item(9) 'Name3
                .Rows(RowNo).Cells(10).Value = dsFSched.Tables(0).Rows(RowNo).Item(10) 'ADP_ID4
                .Rows(RowNo).Cells(11).Value = dsFSched.Tables(0).Rows(RowNo).Item(11) 'Name4
                .Rows(RowNo).Cells(12).Value = dsFSched.Tables(0).Rows(RowNo).Item(12) 'ADP_ID5
                .Rows(RowNo).Cells(13).Value = dsFSched.Tables(0).Rows(RowNo).Item(13) 'Name5
                .Rows(RowNo).Cells(14).Value = dsFSched.Tables(0).Rows(RowNo).Item(14) 'ADP_ID6
                .Rows(RowNo).Cells(15).Value = dsFSched.Tables(0).Rows(RowNo).Item(15) 'Name6
                .Rows(RowNo).Cells(16).Value = dsFSched.Tables(0).Rows(RowNo).Item(16) 'ADP_ID7
                .Rows(RowNo).Cells(17).Value = dsFSched.Tables(0).Rows(RowNo).Item(17) 'Name7
                RowNo = RowNo + 1
            End While
        End With

        If dgvCreateSchedule.RowCount > 0 Then
            dgvCreateSchedule.Rows(0).Selected = True
            dgvCreateSchedule.CurrentCell = dgvCreateSchedule.Rows(0).Cells(0)
            'dgvCreateSchedule.FirstDisplayedScrollingRowIndex = dgvCreateSchedule.CurrentRow.Index
        End If
    Catch ex As Exception
        MessageBox.Show("Cannot open FormattedSchedules to load grid")
    End Try
    AddedNewRow = 0

来自线路的错误消息:.Rows.Add(1)

索引超出范围。必须为非负数且小于集合的大小。" & vbCrLf & "参数名称:索引

【问题讨论】:

    标签: vb.net datagridview row add


    【解决方案1】:

    这应该是最快的选择:

    dgvCreateSchedule.Rows.Clear()
    For Each xrow As DataRow In TempDataTable.dsFSched.Tables(0).Rows
                dgvCreateSchedule.Rows.Add(xrow.ItemArray)
    Next
    

    它的作用是添加所有“单元格”以及行。

    而且在编辑单元格时,我更喜欢使用

    dgvCreateSchedule(y,x).Value = somevalue 
    'Though it's a little bit strange, as it's column first then row for location hence y then x axis , opposed to usual row then column thats x then y axis
    

    【讨论】:

    • 我们发布的时间几乎完全相同
    • 你说得对。在提交之前我没有看到你的答案。
    • 您需要在编辑代码中添加.Value。此外,您在 Row.Add 例程中有一个额外的右括号。
    • @Mary 没错。
    • 我尝试使用答案中建议的两种方法:.Rows.Add(dsFSched.Tables(0).Rows(RowNo).ItemArray) For Each xrow As DataRow In dsFSched.Tables(0)。 Rows dgvCreateSchedule.Rows.Add(xrow.ItemArray) Next 并且都给我同样的错误
    【解决方案2】:

    这样添加,假设列数/顺序相同

    .Rows.Add(dsFSched.Tables(0).Rows(RowNo).ItemArray)
    

    【讨论】:

    • 我尝试使用答案中建议的两种方法: > .Rows.Add(dsFSched.Tables(0).Rows(RowNo).ItemArray) > For Each xrow As DataRow In dsFSched.Tables(0 ).Rows dgvCreateSchedule.Rows.Add(xrow.ItemArray) Next ...并且都给我同样的错误
    【解决方案3】:

    我将 DGV 的名称更改为 DataGridView1,因为这是我在测试项目中碰巧拥有的。

    您可以使用条件编译语句来选择正确的连接字符串。不必在某处保留布尔变量来确定正确的字符串。我知道我会忘记为发布版本更改它。

    您在关闭和处理数据库对象方面做得很好,但如果出现错误,那么所有的好工作都将付诸东流。 Using...End Using 块将完成关闭,即使有错误也会处理。

    将连接字符串直接传递给连接的构造函数,将Sql语句和连接直接传递给命令的构造函数。

    直到最后一分钟才打开您的连接。在DataAdapter.Fill 的情况下,连接会为您打开和关闭,但是,如果适配器找到并打开连接,它将保持打开状态。在这种情况下,不需要适配器或DataSet

    我看不出你的.Rows.Add(1) 有什么问题。问题出现在下一行。 DataGridView.Rows 的索引在 vb.net 中是 Int32Integer,并且您已将 RowNo 声明为 Long。当然,您会想要使用@CruleD 答案建议的代码。

    Private Sub OPCode()
        Dim dt As New DataTable
        Dim FSchedSQL = "Select ShiftName, ScheduleStart, ScheduleEnd, Position, ADP_ID1, Name1, ADP_ID2, Name2, ADP_ID3, Name3, ADP_ID4, Name4, ADP_ID5, Name5, ADP_ID6, Name6, ADP_ID7, Name7 from FormattedSchedules where ScheduleName = @ScheduleName;"
        Try
    #If Not DEBUG Then
            GlobalVariables.strConnection = "CCAPProdConnectionString"
    #Else
            GlobalVariables.strConnection = "CCAPTestConnectionString"
    #End If
            Using FSchedConn As New SqlConnection(ConfigurationManager.ConnectionStrings(GlobalVariables.strConnection).ConnectionString)
                Using FSchedCmd As New SqlCommand(FSchedSQL, FSchedConn)
                    FSchedCmd.Parameters.Add("@ScheduleName", SqlDbType.VarChar).Value = cboCreateScheduleName.Text
                    FSchedConn.Open()
                    dt.Load(FSchedCmd.ExecuteReader)
                End Using
            End Using
            DataGridView1.Rows.Clear()
            For Each xrow As DataRow In dt.Rows
                DataGridView1.Rows.Add(xrow.ItemArray)
            Next
            If DataGridView1.RowCount > 0 Then
                DataGridView1.Rows(0).Selected = True
                DataGridView1.CurrentCell = DataGridView1.Rows(0).Cells(0)
            End If
        Catch ex As Exception
            MessageBox.Show("Error loading grid")
        End Try
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-26
      相关资源
      最近更新 更多