向未绑定的datagridview添加新行

时间:2019-05-23 14:54:23

标签: vb.net datagridview row add

我有一个未绑定的datagridview。由于我正在处理网格中的数据,因此我不想绑定它。这些列是在datagridview的设置(“编辑列”)中预定义的。 我想创建一个新行,然后用数据填充网格行。我正在尝试使用.Add.Rows方法,但是由于{“索引超出范围。必须为非负数并且小于集合的大小。” &vbCrLf&“参数名称:index”} 以下SQL检索数据: 使用CCAP 将@ScheduleName声明为varchar(30)='Walk-In Center April Wk 1 2019' 选择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 来自FormattedSchedules 其中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&”参数名称:index

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

我将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