.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

Refresh a Datagridview

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

I have updated a Datagridview value from textbox in another form. But once i made updated in that form,i can see the changes in DGV without closing and opening an application.

Help me!

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

hi,

just by binding again fresh dataset fetched from the database will solve your problem

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

Where to place fresh dataset in this code,

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
'TODO: This line of code loads data into the 'SqlDataSet2.Persons' table. You can move, or remove it, as needed.
Me.PersonsTableAdapter.Fill(Me.SqlDataSet2.Persons)
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()


txtPer.Text = Person_Details.DataGridView1.CurrentRow.Cells(0).Value.ToString()
txtLas.Text = Person_Details.DataGridView1.CurrentRow.Cells(1).Value.ToString()
txtFir.Text = Person_Details.DataGridView1.CurrentRow.Cells(2).Value.ToString()
txtAdd.Text = Person_Details.DataGridView1.CurrentRow.Cells(3).Value.ToString()
txtCity.Text = Person_Details.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")
myConnection.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try


End Sub


End Class

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

hi,

Write your below code in a function and call that function from load and after update msg in button1_click event

'TODO: This line of code loads data into the 'SqlDataSet2.Persons' table. You can move, or remove it, as needed.
Me.PersonsTableAdapter.Fill(Me.SqlDataSet2.Persons)
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()

Shankey
Author: Maverick             
Posted Date: March 07, 2011     Points: 20   

Hello,

after editing bind ur grid again.

Thanks,
Maverick

Thanks,
Maverick


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