refresh datagrid on another windows form
-
I have two forms in my application.On form1 there is datagridview -dg1 which is filled with a dataset-ds1. On form2 I have a some code ,which after fulfilling certain conditions updates the database table which is contained in ds1. When this is done I want to refresh datagridview-dg1 and show the updated table. Both the forms are already open and I have to update them.
Form2
Public Event refreshdg1(ByVal st As String)
Private Sub
.......Some condition -->True then
....... Update database table'I tried three ways here
1- Raiseevent refreshdg1(s1)
2-
Dim StrCon As String Dim conn As SqlConnection StrCon = ConfigurationManager.AppSettings("StrCon") conn = New SqlConnection(StrCon) conn.Open() Dim ds As New SqlDataAdapter("Select \* from Tag\_History where Tag\_ID = '" + s1 + "' ", conn) Dim commandBuilder As New SqlCommandBuilder(listadapter) Dim table As New DataTable() table.Locale = System.Globalization.CultureInfo.InvariantCulture listadapter.Fill(table) Form1.dg1.DataSource = table conn.Close()
3- Form1.refreshdg(s1)
End sub
Form1:
Addhandler Form2.(I cant refreshdg1 here)
Private Sub refreshdg(ByVal s As String)
Dim f As New Parking_control
AddHandler f.refreshdg1, AddressOf refreshdg
Dim StrCon As String
Dim conn As SqlConnection
StrCon = ConfigurationManager.AppSettings("StrCon")
conn = New SqlConnection(StrCon)
conn.Open()Dim person As String person = s Dim ds1 As New SqlDataAdapter("Select \* from Tag\_History where Name = '" + person + "' ", conn) Dim commandBuilder As New SqlCommandBuilder(ds1) Dim table As New DataTable() table.Locale = System.Globalization.CultureInfo.InvariantCulture listadapter1.Fill(table) table.Columns(0).ColumnName = "Tag ID" table.Columns(1).ColumnName = "Date Time" table.Columns(3).ColumnName = "Tag Status" dg1.DataSource = table conn.Close() End Sub
-
I have two forms in my application.On form1 there is datagridview -dg1 which is filled with a dataset-ds1. On form2 I have a some code ,which after fulfilling certain conditions updates the database table which is contained in ds1. When this is done I want to refresh datagridview-dg1 and show the updated table. Both the forms are already open and I have to update them.
Form2
Public Event refreshdg1(ByVal st As String)
Private Sub
.......Some condition -->True then
....... Update database table'I tried three ways here
1- Raiseevent refreshdg1(s1)
2-
Dim StrCon As String Dim conn As SqlConnection StrCon = ConfigurationManager.AppSettings("StrCon") conn = New SqlConnection(StrCon) conn.Open() Dim ds As New SqlDataAdapter("Select \* from Tag\_History where Tag\_ID = '" + s1 + "' ", conn) Dim commandBuilder As New SqlCommandBuilder(listadapter) Dim table As New DataTable() table.Locale = System.Globalization.CultureInfo.InvariantCulture listadapter.Fill(table) Form1.dg1.DataSource = table conn.Close()
3- Form1.refreshdg(s1)
End sub
Form1:
Addhandler Form2.(I cant refreshdg1 here)
Private Sub refreshdg(ByVal s As String)
Dim f As New Parking_control
AddHandler f.refreshdg1, AddressOf refreshdg
Dim StrCon As String
Dim conn As SqlConnection
StrCon = ConfigurationManager.AppSettings("StrCon")
conn = New SqlConnection(StrCon)
conn.Open()Dim person As String person = s Dim ds1 As New SqlDataAdapter("Select \* from Tag\_History where Name = '" + person + "' ", conn) Dim commandBuilder As New SqlCommandBuilder(ds1) Dim table As New DataTable() table.Locale = System.Globalization.CultureInfo.InvariantCulture listadapter1.Fill(table) table.Columns(0).ColumnName = "Tag ID" table.Columns(1).ColumnName = "Date Time" table.Columns(3).ColumnName = "Tag Status" dg1.DataSource = table conn.Close() End Sub
-
I have two forms in my application.On form1 there is datagridview -dg1 which is filled with a dataset-ds1. On form2 I have a some code ,which after fulfilling certain conditions updates the database table which is contained in ds1. When this is done I want to refresh datagridview-dg1 and show the updated table. Both the forms are already open and I have to update them.
Form2
Public Event refreshdg1(ByVal st As String)
Private Sub
.......Some condition -->True then
....... Update database table'I tried three ways here
1- Raiseevent refreshdg1(s1)
2-
Dim StrCon As String Dim conn As SqlConnection StrCon = ConfigurationManager.AppSettings("StrCon") conn = New SqlConnection(StrCon) conn.Open() Dim ds As New SqlDataAdapter("Select \* from Tag\_History where Tag\_ID = '" + s1 + "' ", conn) Dim commandBuilder As New SqlCommandBuilder(listadapter) Dim table As New DataTable() table.Locale = System.Globalization.CultureInfo.InvariantCulture listadapter.Fill(table) Form1.dg1.DataSource = table conn.Close()
3- Form1.refreshdg(s1)
End sub
Form1:
Addhandler Form2.(I cant refreshdg1 here)
Private Sub refreshdg(ByVal s As String)
Dim f As New Parking_control
AddHandler f.refreshdg1, AddressOf refreshdg
Dim StrCon As String
Dim conn As SqlConnection
StrCon = ConfigurationManager.AppSettings("StrCon")
conn = New SqlConnection(StrCon)
conn.Open()Dim person As String person = s Dim ds1 As New SqlDataAdapter("Select \* from Tag\_History where Name = '" + person + "' ", conn) Dim commandBuilder As New SqlCommandBuilder(ds1) Dim table As New DataTable() table.Locale = System.Globalization.CultureInfo.InvariantCulture listadapter1.Fill(table) table.Columns(0).ColumnName = "Tag ID" table.Columns(1).ColumnName = "Date Time" table.Columns(3).ColumnName = "Tag Status" dg1.DataSource = table conn.Close() End Sub
Wow - what horrible code. Also, depending on where 's' comes from, odds are good that I could erase your database just by using the search functionality. The answer to your original question is delegates, but I'd say you have a long way to go in general before you can write code anyone would want to run, let alone pay for.
Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )
-
He means set up a delegate that is called in form2 and is hooked to a method in form1
Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )
-
I have two forms in my application.On form1 there is datagridview -dg1 which is filled with a dataset-ds1. On form2 I have a some code ,which after fulfilling certain conditions updates the database table which is contained in ds1. When this is done I want to refresh datagridview-dg1 and show the updated table. Both the forms are already open and I have to update them.
Form2
Public Event refreshdg1(ByVal st As String)
Private Sub
.......Some condition -->True then
....... Update database table'I tried three ways here
1- Raiseevent refreshdg1(s1)
2-
Dim StrCon As String Dim conn As SqlConnection StrCon = ConfigurationManager.AppSettings("StrCon") conn = New SqlConnection(StrCon) conn.Open() Dim ds As New SqlDataAdapter("Select \* from Tag\_History where Tag\_ID = '" + s1 + "' ", conn) Dim commandBuilder As New SqlCommandBuilder(listadapter) Dim table As New DataTable() table.Locale = System.Globalization.CultureInfo.InvariantCulture listadapter.Fill(table) Form1.dg1.DataSource = table conn.Close()
3- Form1.refreshdg(s1)
End sub
Form1:
Addhandler Form2.(I cant refreshdg1 here)
Private Sub refreshdg(ByVal s As String)
Dim f As New Parking_control
AddHandler f.refreshdg1, AddressOf refreshdg
Dim StrCon As String
Dim conn As SqlConnection
StrCon = ConfigurationManager.AppSettings("StrCon")
conn = New SqlConnection(StrCon)
conn.Open()Dim person As String person = s Dim ds1 As New SqlDataAdapter("Select \* from Tag\_History where Name = '" + person + "' ", conn) Dim commandBuilder As New SqlCommandBuilder(ds1) Dim table As New DataTable() table.Locale = System.Globalization.CultureInfo.InvariantCulture listadapter1.Fill(table) table.Columns(0).ColumnName = "Tag ID" table.Columns(1).ColumnName = "Date Time" table.Columns(3).ColumnName = "Tag Status" dg1.DataSource = table conn.Close() End Sub
This is a case where you do not need an event. Events are good when one or more objects need to react to another object's behavior. Here one form is telling the other form to update. Therefore, you just need a simple method call. Event handling just complicates the communication between the two forms (objects). Remove all event handling code, such as the
Public Event
,AddHandler
, andRaiseEvent
statements. Just callForm1.refreshdg(s1)
as you did in Item #3. I usually useRefreshUI
as the standard entry point for my forms. If needed, I will addRefreshData
,RefreshButtonState
, and so on. -
Wow - what horrible code. Also, depending on where 's' comes from, odds are good that I could erase your database just by using the search functionality. The answer to your original question is delegates, but I'd say you have a long way to go in general before you can write code anyone would want to run, let alone pay for.
Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )
Why are you being rude? Please remove your comment.
-
This is a case where you do not need an event. Events are good when one or more objects need to react to another object's behavior. Here one form is telling the other form to update. Therefore, you just need a simple method call. Event handling just complicates the communication between the two forms (objects). Remove all event handling code, such as the
Public Event
,AddHandler
, andRaiseEvent
statements. Just callForm1.refreshdg(s1)
as you did in Item #3. I usually useRefreshUI
as the standard entry point for my forms. If needed, I will addRefreshData
,RefreshButtonState
, and so on.