0
这应该是直接前进,但它不想为我工作..填写表单加载数据网格vb.net
我想填充表单加载Datagridview。下面的Sub工作正常,如果我从一个按钮调用Populategrid(),一旦网格填充了Reloadrecords & displayrecords。
Public Sub populategrid()
If Trim(Me.Text) = "CIMDETAILS" Then
da = New SqlDataAdapter("SELECT * from Interactions where [Name] like '" & txtdetect2.Text & "%' order by [IntDate] desc", SQLCon)
dsetAssets = New DataSet
da.Fill(dsetAssets, "Interactions")
dgvData.DataSource = dsetAssets.Tables("Interactions").DefaultView
Else
ReloadRecords()
DisplayRecords()
End If
Public Sub DisplayRecords()
Try
Dim da = New SqlDataAdapter("SELECT * from Interactions order by [IntDate] desc", SQLCon)
dsetAssets = New DataSet
da.Fill(dsetAssets, "Interactions")
dgvData.DataSource = dsetAssets.Tables("Interactions").DefaultView
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
End Try
End Sub
Sub ReloadRecords()
Try
FillCIM("SELECT * FROM Interactions order by [IntDate] desc", dgvData)
dgvData.Columns(0).Visible = False
dgvData.Columns(1).Width = 300 ''Name''
dgvData.Columns(1).HeaderCell.Value = "Customer Name"
dgvData.Columns(2).Width = 200 ''Int type''
dgvData.Columns(2).HeaderCell.Value = "Interaction Type"
dgvData.Columns(3).Width = 170 ''Int Date''
dgvData.Columns(3).HeaderCell.Value = "Interaction Date"
dgvData.Columns(4).Width = 400 ''Remarks''
dgvData.Columns(4).HeaderCell.Value = "Remarks"
dgvData.Columns(5).Visible = False
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
End Try
End Sub
Private Sub CIMDETAILS_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ReloadRecords()
DisplayRecords()
Call populategrid()
End Sub
Public Function FillCIM(ByVal Sqlstring As String, ByVal MyDataGrid As DataGridView)
Dim Constring = "Data Source=" & database & "\SQLEXPRESS; Initial Catalog = CIM; Integrated Security=true"
Dim SQLCon As New SqlConnection(Constring)
Dim SQLAdapter As New SqlDataAdapter()
Dim myDataset As New DataSet()
SQLCon.Open()
Try
SQLAdapter.SelectCommand = New SqlCommand(Sqlstring, SQLCon)
SQLAdapter.Fill(myDataset)
MyDataGrid.DataSource = myDataset.Tables(0)
Catch ex As Exception
End Try
SQLCon.Close()
SQLAdapter.Dispose()
myDataset.Dispose()
Return True
End Function
不要连接SQL字符串,使用参数绑定,除非你想SQL注入攻击 – lad2025
@ lad2025,感谢您的建议!我添加了更多代码来更好地解释我的问题。也许你可以帮忙?我将在此期间查看参数绑定。 – Matt
我不知道vb.net,但我看到PopulateGrid检查me.Text的特定值。可能是这样,在从形式的负载这个价值还没有设置在我的文本? – GuidoG