我的系统有一个小问题,这是我插入数据时的问题,该数据将显示在DatagridView
中,现在,如果我再次插入其他数据,则{{1}中的第一个数据}将被删除,并将被第二个数据替换。听起来很简单,但是我尝试了所有类似的操作,复制了相同的代码,搜索到互联网,但是仍然遇到相同的问题。
我也使用DatagridView
来查看代码去向和触发什么。
到目前为止,这是我的代码。
Breakpoint
当我添加数据并单击按钮显示按钮时。结果还可以。
现在,如果我再次添加数据。第一个数据将删除并替换为第二个数据。
如结果2所示,有一个空白 If TxtRod_ID.Text = "" Then MsgBox("Please enter RD Code.") : Exit Sub
If rbRD.Checked = False And rbOSS.Checked = False Then MsgBox("Please select User On.") : Exit Sub
Dim cn As New OleDbConnection
Dim strUser As String = ""
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
If rbRD.Checked = True Then
RoDID = TxtRod_ID.Text
RdGetSrvr()
cn = New OleDbConnection(RDcon)
Try
If cn.State = ConnectionState.Closed Then cn.Open()
Catch ex As Exception
MessageBox.Show("Cannot connect to the database. Please contact the administrator.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End Try
If rbUserName.Checked = True Then
If txtFname.Text.Trim() = "" And txtLname.Text.Trim() = "" Then MsgBox("Please enter First and/or Last Name of User.") : Exit Sub
strUser = "select * from USERS..MS_User where First_Name like '%" & txtFname.Text.Trim() & "%' and Last_Name like '%" & txtLname.Text.Trim() & "%'"
ElseIf rbUserId.Checked = True Then
If txtUserID.Text.Trim() = "" Then MsgBox("Please enter User ID.") : Exit Sub
strUser = "select * from USERS..MS_User where User_ID like '%" & txtUserID.Text.Trim() & "%'"
End If
Else
If rbUserId.Checked = True Then
If txtUserID.Text.Trim() = "" Then MsgBox("Please enter User ID.") : Exit Sub
COGetSrvr()
cn = New OleDbConnection(COcon)
cn.Open()
strUser = "select * from objUsr..user_locked_tbl where UserName like '%" & txtUserID.Text.Trim() & "%'"
End If
End If
cmd = New OleDbCommand(strUser, cn)
dr = cmd.ExecuteReader()
Dim ctr As Integer = 0
If dr.HasRows Then
While dr.Read
dgUser.Rows.Add()
If rbRD.Checked = True Then
dgUser("dgUserID1", ctr).Value = dr("User_ID")
dgUser("dgFname1", ctr).Value = dr("First_Name")
dgUser("dgLname1", ctr).Value = dr("Last_Name")
dgUser("dgPrimaryRole1", ctr).Value = dr("Primary_Role")
dgUser("dgUserStatus1", ctr).Value = dr("User_Status")
dgUser("dgULoginCnt1", ctr).Value = dr("Unsuccessful_Login_Count")
dgUser("dgDesig1", ctr).Value = dr("Designation")
Else
dgUser("dgUserID1", ctr).Value = dr("UserName")
dgUser("dgFname1", ctr).Value = ""
dgUser("dgLname1", ctr).Value = ""
dgUser("dgPrimaryRole1", ctr).Value = ""
dgUser("dgUserStatus1", ctr).Value = ""
dgUser("dgULoginCnt1", ctr).Value = ""
dgUser("dgDesig1", ctr).Value = ""
End If
End While
Else
MsgBox("No record found.")
Exit Sub
End If
End Sub
,当我将其插入到我的代码row
中时,当然会删除空白dgUser.Rows.Clear
,但我得到了相同的结果问题。
我要实现的是,如果我插入第一个数据,则在插入第二个数据时不会将其删除。
答案 0 :(得分:1)
好像标志“ ctr”在循环中没有改变。 它是关于放置的结果的行号的吗? 那么所有结果都放在同一个地方吗?
答案 1 :(得分:0)
Dim ctr As Integer = 0
If dr.HasRows Then
While dr.Read
For ctr = 0 To dgUser.Rows.Count - 1
Next
dgUser.Rows.Add()
If rbRD.Checked = True Then
dgUser("dgUserID", ctr).Value = If(IsDBNull(dr("User_ID")) = True, "", dr("User_ID"))
dgUser("dgFname", ctr).Value = If(IsDBNull(dr("First_Name")) = True, "", dr("First_Name"))
dgUser("dgLname", ctr).Value = If(IsDBNull(dr("Last_Name")) = True, "", dr("Last_Name"))
dgUser("dgPrimaryRole", ctr).Value = If(IsDBNull(dr("Primary_Role")) = True, "", dr("Primary_Role"))
dgUser("dgUserStatus", ctr).Value = If(IsDBNull(dr("User_Status")) = True, "", dr("User_Status"))
dgUser("dgULoginCnt", ctr).Value = If(IsDBNull(dr("Unsuccessful_Login_Count")) = True, "", dr("Unsuccessful_Login_Count"))
dgUser("dgDesig", ctr).Value = If(IsDBNull(dr("Designation")) = True, "", dr("Designation"))
Else
dgUser("dgUserID", ctr).Value = If(IsDBNull(dr("UserName")) = True, "", dr("UserName"))
dgUser("dgFname", ctr).Value = ""
dgUser("dgLname", ctr).Value = ""
dgUser("dgPrimaryRole", ctr).Value = ""
dgUser("dgUserStatus", ctr).Value = ""
dgUser("dgULoginCnt", ctr).Value = ""
dgUser("dgDesig", ctr).Value = ""
End If
ctr += 1
End While
我明白了。我只是忘记在代码中插入For Loop
。感谢@Terry D的想法。