Programming for Everybody: August 2019

insert update delete data in datagridview without using database in visual basic.net




Imports System.Data.DataTable
Public Class Form2
    Dim table As New DataTable("table")
    Dim index As Integer





    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        table.Columns.Add("ID", Type.GetType("System.Int32"))

        table.Columns.Add("First name", Type.GetType("System.String"))

        table.Columns.Add("Last name", Type.GetType("System.String"))

        table.Columns.Add("Sum", Type.GetType("System.Int32"))


        DataGridView1.DataSource = table







    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        table.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text)

        DataGridView1.DataSource = table




    End Sub

   

  

   

    
    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick

        index = e.RowIndex

        Dim selectedrow As DataGridViewRow


        selectedrow = DataGridView1.Rows(index)



        TextBox1.Text = selectedrow.Cells(0).Value.ToString


        TextBox2.Text = selectedrow.Cells(1).Value.ToString


        TextBox3.Text = selectedrow.Cells(2).Value.ToString



        TextBox4.Text = selectedrow.Cells(3).Value.ToString














    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Dim newdata As DataGridViewRow


        newdata = DataGridView1.Rows(index)


        newdata.Cells(0).Value = TextBox1.Text

        newdata.Cells(1).Value = TextBox2.Text


        newdata.Cells(2).Value = TextBox3.Text



        newdata.Cells(3).Value = TextBox4.Text
















    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Text = ""


        TextBox2.Text = ""



        TextBox3.Text = ""



        TextBox4.Text = ""







    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        DataGridView1.Rows.RemoveAt(index)








    End Sub
End Class

Programming in Visual Basic .Net How to Connect SQL server

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'Database2DataSet.Table1' table. You can move, or remove it, as needed.
        Me.Table1TableAdapter.Fill(Me.Database2DataSet.Table1)

    End Sub

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

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Table1BindingSource.AddNew()

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Table1BindingSource.MoveNext()

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Table1BindingSource.EndEdit()
        Table1TableAdapter.Update(Database2DataSet)
        MessageBox.Show("Data Saved")
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Table1BindingSource.RemoveCurrent()

    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Me.Close()

    End Sub
End Class