23/01/2012

How to Add data from Text box to DataGridViewer? Vb.Net Code for beginners

In the process of application development the need to show the data from Text box to data gridview arises many times so in this post I am posting the code which I used for show the data from text box to datagridview simultaneously. For better understanding of these type of vb.net code or database related inquiry the book written by Mike Murach is very good book which is available on amazon with free shipping charge, I will suggest one can be refer

For showing this, I have designed a windows form which is as under:
In this project I have saved the data which are in text boxes to database and same time I called back that data in datagridviewer  so My Vb.Net code is as follow:
On Form load event:
Imports System.Data.OleDb
Public Class FrmDeplyment
    Inherits System.Windows.Forms.Form
    Dim Cnn As New OleDbConnection
    Dim EmpNoComm As New OleDb.OleDbCommand
    Dim cm As BindingManagerBase
    Dim bm As BindingManagerBase
    Dim editmode As Boolean
    Dim CurRecord As Integer
    Dim TotalRecs As Integer
    Private Sub FrmDeplyment_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Cnn = New OleDb.OleDbConnection
            Cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\IRBn.mdb;Persist Security Info=True;Jet OLEDB:Database Password=123"
            Cnn.Open()
' fill the database
            DaPlaceofDeploy.Fill(Dsdeployment1, "placeofdeplyment")
            DaDeploy.Fill(Dsdeployment1, "Deployment")
            TotalRecs = Dsdeployment1.Tables("Deployment").Rows.Count - 1
Private sub function to call the save data in datagridviewer
    Private Sub gridfill()
       
            Dim dv As New DataView
            dv = New DataView(Dsdeployment1.Tables("Deployment"))
            dv.RowFilter = "MovementOrderNo='" & TxtMOrderNo.Text & "'"
            DataGridView1.DataSource = dv
       
end sub

On Save button click even

   TotalRecs = Dsdeployment1.Tables("Deployment").Rows.Count - 1
                       If isValidData() Then
                       Dim pKey As String
                        pKey = Txtsno1.Text
          dr = Dsdeployment1.Tables("Deployment").Rows.Find(pKey)
                        dr.BeginEdit()
                        dr(1) = TxtMOrderNo.Text
                        dr(2) = DtpMOrderdate.Value.Date
                        dr(3) = CbPlaceofDeployment.Text
                        dr(4) = DtpDeploymentdate.Value.Date
                        dr(5) = DtpDateofReturn.Value.Date
                   dr(6) = TxtDeploymentauthorityno.Text
                  dr(7) = DtpDeploymentauthoritydate.Value.Date
                        dr(8) = TxtPisno.Text
                        dr(9) = txtname.Text
                        dr(10) = txtrank.Text
                        dr(11) = txtcoy.Text
                        dr(12) = txtaa.Text
                        dr(13) = txtremarks.Text
                        dr.EndEdit()
                        MsgBox("Record  Edited & Saved")
                        gridfill()
                        Btnupdate.Enabled = True

                    Else
               dr = Dsdeployment1.Tables("Deployment").NewRow
                        dr(0) = Txtsno1.Text
                        dr(1) = TxtMOrderNo.Text
                        dr(2) = DtpMOrderdate.Value.Date
                        dr(3) = CbPlaceofDeployment.Text
                        dr(4) = DtpDeploymentdate.Value.Date
                        dr(5) = DtpDateofReturn.Value.Date
                        dr(6) = TxtDeploymentauthorityno.Text
             dr(7) = DtpDeploymentauthoritydate.Value.Date
                        dr(8) = TxtPisno.Text
                        dr(9) = txtname.Text
                        dr(10) = txtrank.Text
                        dr(11) = txtcoy.Text
                        dr(12) = txtaa.Text
                        dr(13) = txtremarks.Text
               Dsdeployment1.Tables("Deployment").Rows.Add(dr)
                        gridfill()
                        MsgBox("Record Saved")
                        TotalRecs += 1
                        
                      
                    End If
                End If
            End If
            End Sub

Code for Update Button click event

        Try
                If Dsdeployment1.HasChanges Then
                DaDeploy.Update(Dsdeployment1, "Deployment")
                MsgBox(" Record Saved ")
                TxtMOrderNo.Text = ""
            End If
       
    End Sub
Code for Exit Button

Me. Close
Click to know the ful code of this project
you can  purchase this best book for more such code: