[英]Adding new row to unbound datagridview
I have an unbound datagridview. 我有一个未绑定的datagridview。 Because of the various things I am doing with the data in the grid I do not want to bind it. 由于我正在处理网格中的数据,因此我不想绑定它。 The columns are predefined in the settings (Edit Columns) of the datagridview. 这些列是在datagridview的设置(“编辑列”)中预定义的。 I want to create a new row and then populate the grid row with data. 我想创建一个新行,然后用数据填充网格行。 I am trying to use the .Add.Rows
method but it is failing with 我正在尝试使用.Add.Rows
方法,但操作失败
{"Index was out of range. Must be non-negative and less than the size of the collection." {“索引超出范围。必须是非负数并且小于集合的大小。” & vbCrLf & "Parameter name: index"} &vbCrLf&“参数名称:index”}
The following SQL retrieves data: 以下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;
and the rowcount is greater than 0 so it is getting results. 并且行数大于0,因此正在获得结果。 I do not understand what index is out of range or why the collection is 0 Code is below: 我不明白什么索引超出范围或为什么集合为0代码如下:
Tried .Rows.Add(1)
and .Rows.Add()
and .Rows.Add("")
试过.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
Error message from line: .Rows.Add(1)
来自以下行的错误消息: .Rows.Add(1)
Index was out of range. 索引超出范围。 Must be non-negative and less than the size of the collection." & vbCrLf & "Parameter name: index 必须为非负数并且小于集合的大小。“&vbCrLf&”参数名称:index
This should be the fastest option: 这应该是最快的选择:
dgvCreateSchedule.Rows.Clear()
For Each xrow As DataRow In TempDataTable.dsFSched.Tables(0).Rows
dgvCreateSchedule.Rows.Add(xrow.ItemArray)
Next
What it does adds all "Cells" along with row. 它的作用是将所有“单元格”与行一起添加。
And when editing cells, I prefer to use 在编辑单元格时,我更喜欢使用
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
假设有相同的列数/顺序,则以这种方式添加
.Rows.Add(dsFSched.Tables(0).Rows(RowNo).ItemArray)
I changed the name of the DGV to DataGridView1
because that is what I happened to have in my test project. 我将DGV的名称更改为DataGridView1
因为这正是我在测试项目中遇到的。
You can use conditional compile statements to chose the correct connection string. 您可以使用条件编译语句来选择正确的连接字符串。 Not necessary to keep a Boolean variable somewhere to determine correct string. 不必将布尔变量保留在某个位置以确定正确的字符串。 I know I would forget to change it for the release version. 我知道我会忘记为发布版本进行更改。
You did a good job closing and disposing of database objects but if there is an error all that good work will be for naught. 您在关闭和处理数据库对象方面做得很好,但是如果出现错误,那么所有的好工作都将一事无成。 A Using...End Using
block will accomplish the close, dispose even if there is an error. Using...End Using
块将完成关闭,即使发生错误也要进行处置。
Pass the connection string directly to the constructor of the connection and pass the Sql statement and the connection directly to the constructor of the command. 将连接字符串直接传递给连接的构造函数,并将Sql语句和连接直接传递给命令的构造函数。
Don't open your connection until the last minute. 直到最后一分钟才打开连接。 In the case of a DataAdapter.Fill
, the connection is opened and closed for you however, if the adapter finds and open connection it leaves it open. 如果是DataAdapter.Fill
,则为您打开和关闭连接,但是如果适配器找到并打开了连接,它将保持打开状态。 In this case there is no need for an adapter or a DataSet
. 在这种情况下,没有必要适配器或DataSet
。
I do not see anything wrong with your line .Rows.Add(1)
. 我看不到您的行.Rows.Add(1)
有什么问题。 The problem comes on the next line. 问题出在下一行。 The index of DataGridView.Rows
is an Int32
, Integer
in vb.net, and you have declared RowNo
as Long
. DataGridView.Rows
的索引是vb.net中的Int32
, Integer
,并且已将RowNo
声明为Long
。 Of course you will want to use the code suggested by @CruleD answer. 当然,您将要使用@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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.