这是我的功能,用于将数据从sql server显示到Datagridview
Private Function getpdfinfo1() As DataTable
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
Dim dtpi As New DataTable
Dim connString As String = ConfigurationManager.ConnectionStrings("pdflib.My.MySettings.hazimdbConnectionString").ConnectionString
Using conn As New SqlConnection(connString)
Using cmd As New SqlCommand("select idpdf as ID,pdfname as Title, categories as Categories , extension as Extension from pdfinfo where ( username='" & Label10.Text & "' ) ", conn)
conn.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
dtpi.Load(reader)
conn.Close()
End Using
End Using
Return dtpi
End Function
在主表单加载中,甚至我都按如下所示调用函数getpdfinfo1()
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1.DataSource = getpdfinfo1()
End Sub
我从其他表单保存数据。按钮单击事件以保存数据,如下所示
cmd = New SqlCommand("Insert into pdfinfo (pdfname,pdfdata,categories,username,iduser,extension) values (@pdfname,@pdfdata,@categories,@username,@iduser,@extension) ", connection)
connection.Open()
cmd.Parameters.Add(New SqlParameter("@pdfname", SqlDbType.NVarChar, 100)).Value = TextBox2.Text
cmd.Parameters.Add(New SqlParameter("@categories", SqlDbType.NVarChar, 100)).Value = ComboBox1.Text
cmd.Parameters.Add(New SqlParameter("@username", SqlDbType.NVarChar, 100)).Value = TextBox4.Text
cmd.Parameters.Add(New SqlParameter("@iduser", SqlDbType.Int)).Value = Label6.Text
cmd.Parameters.Add(New SqlParameter("@extension", SqlDbType.NChar, 10)).Value = Path.GetExtension(ofd.FileName)
Dim fs As New FileStream(ofd.FileName, FileMode.Open, FileAccess.Read)
Dim br As New BinaryReader(fs)
Dim file() As Byte = br.ReadBytes(br.BaseStream.Length)
cmd.Parameters.Add(New SqlParameter("@pdfdata", SqlDbType.VarBinary)).Value = file
cmd.ExecuteNonQuery()
Label8.Text = "Upload Completed"
所以我想在(保存数据表单)中单击“保存”按钮,然后 主表格(表格2)中的Datagridview **将自动更新。
或者,如何在Sql数据更改时创建函数更新Datagridview。
答案 0 :(得分:0)
getpdfinfo1函数是私有的,将其设为公共函数,然后在“保存”按钮单击事件中调用此函数。要从其他表单调用函数,您可以在表单name.function前面加上前缀。
答案 1 :(得分:0)