.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> VB.Net >> Post New QuestionBookmark and Share Subscribe to Forum

DataGridview update

Posted By: srini     Posted Date: February 07, 2011    Points:2   Category :VB.Net
Hi,

I have a form have a datagridview and inside this form have a button to call another form to update the datagridview's data.

I need to refresh the parent,when the 2nd form is close.

Could anyone please help me.

Thanks
Srini.

Responses
Author: shankey             
Posted Date: February 08, 2011     Points: 5   

hi,

You want to refresh means you want to get latest data for the grid view, then bind the datagrid view again with the fresh dataset fetch from db.

Please correct me if i am wrong.

Shankey
Author: srini             
Posted Date: February 08, 2011     Points: 5   

Hi Shankey,

Thanks for your kind response.

Actually, i have two forms. One form for Save the details and Datagridview and other form for updating the details in DGV,once i selected the particular row.

These things are working good. Now i need to show the DGV with the latest changes what i have done in the second form called update.

Hope you understand what i am saying!!

Thanks,
Srini.

Srini
Author: shankey             
Posted Date: February 08, 2011     Points: 5   

hi,

you can write your code which binds the dgv to a dataset in a function and this function should fetch dataset from the database every time it is called and should be common in the sense that it is accessible from both the form. then after your update is performed then call that function which is common and which binds your dgv to the dataset fetched from the database.

Shankey
Author: srini             
Posted Date: February 08, 2011     Points: 5   

Code for form1 :

Imports System.Data.SqlClient

Public Class Person_details1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Edit_Form2.Show()
End Sub

Private Sub Person_details1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim conn As SqlConnection
Dim da As SqlDataAdapter
Dim ds As DataSet
conn = New SqlConnection("server=.\SQLExpress;Initial Catalog=sql;Integrated Security=True")
conn.Open()
da = New SqlDataAdapter("select * from persons ", conn)
ds = New DataSet("persons")
da.Fill(ds, "persons")
DataGridView1.DataSource = ds.Tables("persons")
conn.Close()

End Sub


Code for form2 :

Imports System.Data.SqlClient

Public Class Edit_Form2
Inherits System.Windows.Forms.Form
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim ra As Integer

Private Sub Edit_Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim conn As SqlConnection
Dim da As SqlDataAdapter
Dim ds As DataSet
conn = New SqlConnection("server=.\SQLExpress;Initial Catalog=sql;Integrated Security=True")
conn.Open()
da = New SqlDataAdapter("select * from persons ", conn)
ds = New DataSet("persons")
da.Fill(ds, "persons")
DataGridView1.DataSource = ds.Tables("persons")
conn.Close()
'TODO: This line of code loads data into the 'SqlDataSet2.Persons' table. You can move, or remove it, as needed.
txtPer.Text = Person_details1.DataGridView1.CurrentRow.Cells(0).Value.ToString()
txtLas.Text = Person_details1.DataGridView1.CurrentRow.Cells(1).Value.ToString()
txtFir.Text = Person_details1.DataGridView1.CurrentRow.Cells(2).Value.ToString()
txtAdd.Text = Person_details1.DataGridView1.CurrentRow.Cells(3).Value.ToString()
txtCity.Text = Person_details1.DataGridView1.CurrentRow.Cells(4).Value.ToString()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

myConnection = New SqlConnection("server=.\SQLExpress;Initial Catalog=sql;Integrated Security=True")
myCommand = New SqlCommand("update persons set LastName= '" + txtLas.Text + "', FirstName='" + txtFir.Text + "' where P_Id=" + txtPer.Text + " ", myConnection)

Try
myConnection.Open()
ra = myCommand.ExecuteNonQuery()
myConnection.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
myConnection = New SqlConnection("server=.\SQLExpress;Initial Catalog=sql;Integrated Security=True")
myCommand = New SqlCommand("update persons set Address='" + txtAdd.Text + "',City='" + txtCity.Text + "' where P_Id=" + txtPer.Text + " ", myConnection)

Try
myConnection.Open()
ra = myCommand.ExecuteNonQuery()
MessageBox.Show("Updated")
Dim conn As SqlConnection
Dim da As SqlDataAdapter
Dim ds As DataSet
conn = New SqlConnection("server=.\SQLExpress;Initial Catalog=sql;Integrated Security=True")
conn.Open()
da = New SqlDataAdapter("select * from persons ", conn)
ds = New DataSet("persons")
da.Fill(ds, "persons")
DataGridView1.DataSource = ds.Tables("persons")
conn.Close()
myConnection.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub


Help me what to do now!!!

Srini
Author: shankey             
Posted Date: February 08, 2011     Points: 5   

hi i will create a smple app and give you soon but can you give script of the tables related to these grid

Shankey
Author: srini             
Posted Date: February 08, 2011     Points: 5   

Hi,,

CREATE TABLE [dbo].[Persons](
[P_Id] [int] NOT NULL,
[LastName] [varchar](255) NOT NULL,
[FirstName] [varchar](255) NULL,
[Address] [varchar](255) NULL,
[City] [varchar](255) NULL
)


Thanks,
Srini.

Srini
Author: srini             
Posted Date: February 08, 2011     Points: 5   

Hi Shankey,

Did you find the answer for my question?

Srini


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend