Question refresh the datagridview after inserting?

tuanluc108

New member
Joined
Dec 13, 2011
Messages
1
Programming Experience
Beginner
hello everyone
:friendly_wink::friendly_wink:
im just a new member of forum, and today I got one question so that 's why im here :apathy:. And I hope you people can help me to figure it out.

Actually now, I have one datagridview in the form, and it also displays my data from the table:

VB.NET:
'open the connection
        con = New SqlConnection("server=LEX-PC\SQLEXPRESS;trusted_connection=yes;database=MobileManagement")

        'fill the data to the datagridview
    cmd = New SqlCommand("select * from Mobile", con)
        da = New SqlDataAdapter(cmd)
        da.Fill(ds, "Mobile")
        Me.DataGridView1.DataSource = ds
        Me.DataGridView1.DataMember = "Mobile"

And I have one insert button to insert the data into my table also.

VB.NET:
cmdInsert = New SqlCommand("insert into Mobile values (@MobileCode, @MobileCompany)", con)
        con.Open()
        cmdInsert.Parameters.Add("@MobileCode", SqlDbType.VarChar)
        cmdInsert.Parameters("@MobileCode").Value = txtCode.Text
        cmdInsert.Parameters.Add("@MobileCompany", SqlDbType.VarChar)
        cmdInsert.Parameters("@MobileCompany").Value = txtCompany.Text
        cmdInsert.ExecuteNonQuery()

        MessageBox.Show("New mobile was inserted", "Insert", MessageBoxButtons.OK, MessageBoxIcon.Information)


Inserting data into the table is done, it worked very well. But now what i want is, whenever I insert data into my table, the data I just inserted is displayed immediately to my datagrid. I also try binding the datasource of datagrid view again, or datagrid.refresh also, but it does not work. So I hope some one can help me. Sorry if I express it not too clearly. Thanks for reading!
 
I don't remember how forms work but my guess is you'd have to re-run that top section of code.
 
Back
Top