Hi,
As a learning exercise I have created a simple form which contains a
DataGridView, a Textbox and a Command Button. The DGW is populated from a
stored procedure which optionally takes a parameter to filter the data
retrieved.
When the form is first loaded, the DGV is populated from the procedure with
no parameter - I've managed to get this bit working fine.
What I'd like to happen next is for the user to enter a reference ("visit")
in the text box and when the user clicks the command button, the data in the
DGV is refreshed as per the procedure executing with the parameter.
However, I'm unable to do this, I just can't get the data to refresh. I'm
not sure at what stage I should be refreshing the data and who I achieve
it...
My form consists of a single DGV, one textbox and one command button. I
haven't included any error handling or such like.
I'd be much appreciative if someone could give me a few pointers as to how
to get the form to refresh as described above.
I hope I've managed to explain this clearly enough!
Many thanks
Chris.
*************** ***********
Public Class Form1
Dim cnn As New SqlClient.SqlCo nnection( _
"Data Source=COMPUTER 19\CSTEST;" & _
"Initial Catalog=ShipsPe rf_Test;" & _
"Integrated Security=True")
Dim bs As New BindingSource()
Dim cmd As New SqlClient.SqlCo mmand()
Dim prm As New SqlClient.SqlPa rameter
Dim da As New SqlClient.SqlDa taAdapter()
Dim data As New DataSet()
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
cmd.Connection = cnn
cmd.CommandText = "spGangsInvolve dWithVisitSelec t"
cmd.CommandType = CommandType.Sto redProcedure
da.SelectComman d = cmd
da.Fill(Data, "Visits")
bs.DataSource = Data
bs.DataMember = "Visits"
Me.DataGridView 1.DataSource = bs
With Me.DataGridView 1
.AutoResizeColu mns()
.AutoResizeRows ()
End With
End Sub
Private Sub cmdRefresh_Clic k(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles cmdRefresh.Clic k
cmd.Connection = cnn
cmd.CommandText = "spGangsInvolve dWithVisitSelec t"
cmd.CommandType = CommandType.Sto redProcedure
prm = cmd.Parameters. Add("@Visit", SqlDbType.Int)
prm.Value = Me.txtVisit.Tex t
da.SelectComman d = cmd
bs.DataSource = data
bs.DataMember = "Visits"
Me.DataGridView 1.DataSource = bs
Me.DataGridView 1.Refresh()
End Sub
End Class
As a learning exercise I have created a simple form which contains a
DataGridView, a Textbox and a Command Button. The DGW is populated from a
stored procedure which optionally takes a parameter to filter the data
retrieved.
When the form is first loaded, the DGV is populated from the procedure with
no parameter - I've managed to get this bit working fine.
What I'd like to happen next is for the user to enter a reference ("visit")
in the text box and when the user clicks the command button, the data in the
DGV is refreshed as per the procedure executing with the parameter.
However, I'm unable to do this, I just can't get the data to refresh. I'm
not sure at what stage I should be refreshing the data and who I achieve
it...
My form consists of a single DGV, one textbox and one command button. I
haven't included any error handling or such like.
I'd be much appreciative if someone could give me a few pointers as to how
to get the form to refresh as described above.
I hope I've managed to explain this clearly enough!
Many thanks
Chris.
*************** ***********
Public Class Form1
Dim cnn As New SqlClient.SqlCo nnection( _
"Data Source=COMPUTER 19\CSTEST;" & _
"Initial Catalog=ShipsPe rf_Test;" & _
"Integrated Security=True")
Dim bs As New BindingSource()
Dim cmd As New SqlClient.SqlCo mmand()
Dim prm As New SqlClient.SqlPa rameter
Dim da As New SqlClient.SqlDa taAdapter()
Dim data As New DataSet()
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
cmd.Connection = cnn
cmd.CommandText = "spGangsInvolve dWithVisitSelec t"
cmd.CommandType = CommandType.Sto redProcedure
da.SelectComman d = cmd
da.Fill(Data, "Visits")
bs.DataSource = Data
bs.DataMember = "Visits"
Me.DataGridView 1.DataSource = bs
With Me.DataGridView 1
.AutoResizeColu mns()
.AutoResizeRows ()
End With
End Sub
Private Sub cmdRefresh_Clic k(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles cmdRefresh.Clic k
cmd.Connection = cnn
cmd.CommandText = "spGangsInvolve dWithVisitSelec t"
cmd.CommandType = CommandType.Sto redProcedure
prm = cmd.Parameters. Add("@Visit", SqlDbType.Int)
prm.Value = Me.txtVisit.Tex t
da.SelectComman d = cmd
bs.DataSource = data
bs.DataMember = "Visits"
Me.DataGridView 1.DataSource = bs
Me.DataGridView 1.Refresh()
End Sub
End Class
Comment