首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >向未绑定的datagridview添加新行

向未绑定的datagridview添加新行
EN

Stack Overflow用户
提问于 2019-05-23 22:54:23
回答 3查看 511关注 0票数 0

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

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

下面的SQL检索数据:

代码语言:javascript
复制
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("")

代码语言:javascript
复制
    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 &”参数名称:索引

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56277935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档