Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. How can I add multiple new row into datagrid ?

How can I add multiple new row into datagrid ?

Scheduled Pinned Locked Moved Visual Basic
questioncsharphelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Eunice VB junior
    wrote on last edited by
    #1

    Hi everyone , I'm using vb.net 2003 (window application). I have 2 textbox and want to add these 2 text into datagrid. But when i click "Add" Button for the 1st time, no problem. But after i click the 2 times, it replaced with the 1st row. But what i want is to add them into 2nd or subsequent rows depending on how many times i click 'Add' button.The 1st row remain. Here i attach with my codes. I hope someone can hint me where is my mistake. Thanks in advanced. Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd3.Click Dim Table1 As DataTable Table1 = New DataTable("LotNumber") Dim Row As DataRow Try Dim HBarCode As DataColumn = New DataColumn("HBarCode") HBarCode.DataType = System.Type.GetType("System.String") Table1.Columns.Add(HBarCode) Dim EMIBarCode As DataColumn = New DataColumn("EMIBarCode") EMIBarCode.DataType = System.Type.GetType("System.String") Table1.Columns.Add(EMIBarCode) Row = Table1.NewRow() Row.Item("HBarCode") = Me.txtHBarCode.Text Row.Item("EMIBarCode") = Me.txtEMIBarCode.Text Table1.Rows.Add(Row) Catch ex As Exception MessageBox.Show(ex.Message) End Try Dim ds As New DataSet ds = New DataSet ds.Tables.Add(Table1) dgResult.SetDataBinding(ds, "LotNumber") End Sub thanks, eunice

    C 1 Reply Last reply
    0
    • E Eunice VB junior

      Hi everyone , I'm using vb.net 2003 (window application). I have 2 textbox and want to add these 2 text into datagrid. But when i click "Add" Button for the 1st time, no problem. But after i click the 2 times, it replaced with the 1st row. But what i want is to add them into 2nd or subsequent rows depending on how many times i click 'Add' button.The 1st row remain. Here i attach with my codes. I hope someone can hint me where is my mistake. Thanks in advanced. Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd3.Click Dim Table1 As DataTable Table1 = New DataTable("LotNumber") Dim Row As DataRow Try Dim HBarCode As DataColumn = New DataColumn("HBarCode") HBarCode.DataType = System.Type.GetType("System.String") Table1.Columns.Add(HBarCode) Dim EMIBarCode As DataColumn = New DataColumn("EMIBarCode") EMIBarCode.DataType = System.Type.GetType("System.String") Table1.Columns.Add(EMIBarCode) Row = Table1.NewRow() Row.Item("HBarCode") = Me.txtHBarCode.Text Row.Item("EMIBarCode") = Me.txtEMIBarCode.Text Table1.Rows.Add(Row) Catch ex As Exception MessageBox.Show(ex.Message) End Try Dim ds As New DataSet ds = New DataSet ds.Tables.Add(Table1) dgResult.SetDataBinding(ds, "LotNumber") End Sub thanks, eunice

      C Offline
      C Offline
      CKnig
      wrote on last edited by
      #2

      Ok - the problem is that you allways create a new DataTable whenever you click add. Put the section with the creation of the datatable / dataset to Form_Load or some other initialiser (you only want to call this once or when you need a clean DataTable) (Set the DataTable to be a private member of your class for access) So you should get: private myTable as DataTable = nothing public Sub InitGrid() myTable = New DataTable("LotNumber") Dim HBarCode As DataColumn = New DataColumn("HBarCode") HBarCode.DataType = System.Type.GetType("System.String") myTable .Columns.Add(HBarCode) Dim EMIBarCode As DataColumn = New DataColumn("EMIBarCode") EMIBarCode.DataType = System.Type.GetType("System.String") myTable .Columns.Add(EMIBarCode) Dim ds As New DataSet ds = New DataSet ds.Tables.Add(myTable) dgResult.SetDataBinding(ds, "LotNumber") end sub and Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd3.Click if myTable is nothing then InitGrid() Dim Row As DataRow Try Row = myTable.NewRow() Row.Item("HBarCode") = Me.txtHBarCode.Text Row.Item("EMIBarCode") = Me.txtEMIBarCode.Text myTable.Rows.Add(Row) Catch ex As Exception MessageBox.Show(ex.Message) End Try dgResult.Update() End Sub

      E 2 Replies Last reply
      0
      • C CKnig

        Ok - the problem is that you allways create a new DataTable whenever you click add. Put the section with the creation of the datatable / dataset to Form_Load or some other initialiser (you only want to call this once or when you need a clean DataTable) (Set the DataTable to be a private member of your class for access) So you should get: private myTable as DataTable = nothing public Sub InitGrid() myTable = New DataTable("LotNumber") Dim HBarCode As DataColumn = New DataColumn("HBarCode") HBarCode.DataType = System.Type.GetType("System.String") myTable .Columns.Add(HBarCode) Dim EMIBarCode As DataColumn = New DataColumn("EMIBarCode") EMIBarCode.DataType = System.Type.GetType("System.String") myTable .Columns.Add(EMIBarCode) Dim ds As New DataSet ds = New DataSet ds.Tables.Add(myTable) dgResult.SetDataBinding(ds, "LotNumber") end sub and Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd3.Click if myTable is nothing then InitGrid() Dim Row As DataRow Try Row = myTable.NewRow() Row.Item("HBarCode") = Me.txtHBarCode.Text Row.Item("EMIBarCode") = Me.txtEMIBarCode.Text myTable.Rows.Add(Row) Catch ex As Exception MessageBox.Show(ex.Message) End Try dgResult.Update() End Sub

        E Offline
        E Offline
        Eunice VB junior
        wrote on last edited by
        #3

        Hi CKnig, Thanks a lot. It works now. Appreciate that. cheers, eunice

        1 Reply Last reply
        0
        • C CKnig

          Ok - the problem is that you allways create a new DataTable whenever you click add. Put the section with the creation of the datatable / dataset to Form_Load or some other initialiser (you only want to call this once or when you need a clean DataTable) (Set the DataTable to be a private member of your class for access) So you should get: private myTable as DataTable = nothing public Sub InitGrid() myTable = New DataTable("LotNumber") Dim HBarCode As DataColumn = New DataColumn("HBarCode") HBarCode.DataType = System.Type.GetType("System.String") myTable .Columns.Add(HBarCode) Dim EMIBarCode As DataColumn = New DataColumn("EMIBarCode") EMIBarCode.DataType = System.Type.GetType("System.String") myTable .Columns.Add(EMIBarCode) Dim ds As New DataSet ds = New DataSet ds.Tables.Add(myTable) dgResult.SetDataBinding(ds, "LotNumber") end sub and Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd3.Click if myTable is nothing then InitGrid() Dim Row As DataRow Try Row = myTable.NewRow() Row.Item("HBarCode") = Me.txtHBarCode.Text Row.Item("EMIBarCode") = Me.txtEMIBarCode.Text myTable.Rows.Add(Row) Catch ex As Exception MessageBox.Show(ex.Message) End Try dgResult.Update() End Sub

          E Offline
          E Offline
          Eunice VB junior
          wrote on last edited by
          #4

          Hi CKnig, Can you advice me how should i write the code if i want to save the data from datagrid to database? eg: Let's say i had 'Add' 10 rows. I want to add these 10 rows into database. How should i do? I have been searching for codes. But the result not what i want. Can you pls help. Thanks in advanced. Here is my codes that stuck half way:- Dim strQuery1 As String strQuery1 = "Insert into MMBarCode (HBarCode,EMIBarCode) values('" & DataTable.Rows(HBarCode) & "','" & DataTable.Rows(EMIBarCode) & "')" myConnection.Open() myCommand = New SqlCommand(strQuery1, myConnection) Try ra = myCommand.ExecuteNonQuery() myConnection.Close() Me.Close() Catch ex As Exception myConnection.Close() MessageBox.Show(ex.Message) End Try Thanks, eunice

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups