Results 1 to 11 of 11

Thread: how to automatically add more rows in a datagrid on button click

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    490

    Question how to automatically add more rows in a datagrid on button click

    i want to make a table with five columns in form1

    first name last name title phone email

    the information ie the rows would be filled based on data in another form say form 2 which has all these informations in 5 textboxes.
    in the form2 there will be a button to add all the information of the five textboxes into the form1 table under each column.

    how do i go about it.

    will i have to use datagrid ,dataview, if yes how ?


    kindly help
    cheers
    Last edited by indiewolf; Aug 17th, 2006 at 09:55 AM.
    visual basic.net (2008)

    .net framework 3.5


  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    490

    Re: how to make table so that data is added to it on button click event

    please help.
    i dragged a datagrid.
    created a button in the other form but how do i export data from textboxes in one form to a table in another form?
    visual basic.net (2008)

    .net framework 3.5


  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to make table so that data is added to it on button click event

    Read the "Forms in VB.NET" tutorial in my signature.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    490

    Re: how to make table so that data is added to it on button click event

    thanks j
    i have read, and used that before.
    but i am confused as to how will i communicate with the tables (datagrid)
    when i press the button in form2 , with my motto being to transfer values of textboxes in form2 to this datagrid in form1.

    kindly help
    cheers
    visual basic.net (2008)

    .net framework 3.5


  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    490

    Re: how to make table so that data is added to it on button click event

    ok i am able to communicate now.

    but the problem is wheni click the button after entering the values once they are displayed on the datagrid. fine...but when i enter new values in the textbox and click the button those values are updated.
    ie instead of printing the values in the second row the values are printed in the first row.

    ie only the first row is getting updated.wheras values should be added in different rows.

    kindly help
    cheers
    visual basic.net (2008)

    .net framework 3.5


  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    490

    Re: how to automatically add more rows in a datagrid on button click

    this is my code

    VB Code:
    1. Dim Table1 As DataTable
    2.         Table1 = New DataTable("Contacts")
    3.         'creating a table named contacts
    4.         ' DataGrid1.ReadOnly = True
    5.         'so that no changes are made to the data grid
    6.  
    7.         Dim Row1, Row2, Row3, Row4, Row5 As DataRow
    8.         'declaring five rows for the table
    9.         Try
    10.             Dim Name As DataColumn = New DataColumn("First Name")
    11.             'declaring a column named First Name
    12.             Name.DataType = System.Type.GetType("System.String")
    13.             'setting the datatype for the column
    14.             Table1.Columns.Add(Name)
    15.             'adding the column to table
    16.             Dim Last As DataColumn = New DataColumn("Last Name")
    17.             Last.DataType = System.Type.GetType("System.String")
    18.             Table1.Columns.Add(Last)
    19.             Dim Title As DataColumn = New DataColumn("Title")
    20.             Title.DataType = System.Type.GetType("System.String")
    21.             Table1.Columns.Add(Title)
    22.  
    23.             Dim phone As DataColumn = New DataColumn("Phone")
    24.             phone.DataType = System.Type.GetType("System.String")
    25.             Table1.Columns.Add(phone)
    26.  
    27.             Dim Email As DataColumn = New DataColumn("Email")
    28.             Email.DataType = System.Type.GetType("System.String")
    29.             Table1.Columns.Add(Email)
    30.  
    31.  
    32.  
    33.             Row1 = Table1.NewRow()
    34.             'declaring a new row
    35.             Row1.Item("First Name") = TextBox3.Text
    36.             'filling the row with values. Item property is used to set the field value.
    37.             Row1.Item("Last Name") = TextBox4.Text
    38.             'filling the row with values. adding Last Name
    39.             Row1.Item("Title") = TextBox2.Text
    40.             'filling the row with values. adding a title
    41.             Row1.Item("Phone") = TextBox5.Text
    42.             'filling the row with phone number
    43.             Row1.Item("Email") = TextBox6.Text
    44.  
    45.             'filling the row with email id
    46.             Table1.Rows.Add(Row1)
    47.             'adding the completed row to the table
    48.  
    49.         Catch
    50.         End Try
    51.         Dim ds As New DataSet
    52.         ds = New DataSet
    53.         'creating a dataset
    54.         ds.Tables.Add(Table1)
    55.         'adding the table to dataset
    56.         f1.DataGrid1.SetDataBinding(ds, "Contacts")
    57.         'binding the table to datagrid
    58.  
    59.  
    60.  
    61.         f1.Show()


    the data in textboxes is added to datagrid1 in form1
    now when i write fresh data to textboxes and click the button the first row is rewritten, but i wanted the first row to remain as it is and the data to go to the second row.
    what should i do?

    kindly help
    cheers
    visual basic.net (2008)

    .net framework 3.5


  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to automatically add more rows in a datagrid on button click

    You are creating a completely new DataTable that has no knowledge of any previous data. If you want to keep the original data then you have to use the original DataTable and add the new data to it. Let's say that you had a piece of paper and wrote "Hello" on it. If you then got a new piece of paper and wrote "Goodbye" on it, would you be surprised that it only had "Goodbye" and not "Hello"? If you wanted "Hello" and "Goodbye" then you'd have to write them on the same piece of paper. Same applies here. If you want both the old record and new record then you have toput them in the same DataTable.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    490

    Re: how to automatically add more rows in a datagrid on button click

    i do understand that.
    but the prob is when i remove the "new datatable2 code bit
    it gives me error.


    i get error when i click the button. the error is in the code u gave me for changing the width of the column.
    the error is here :

    visual basic code:--------------------------------------------------------------------------------
    VB Code:
    1. ds.Tables.Add(Table1)
    --------------------------------------------------------------------------------

    Quote:
    Additional information: 'table' argument cannot be null.



    my full code now is :

    this is under the button click event in form2.


    visual basic code:--------------------------------------------------------------------------------
    VB Code:
    1. Dim Table1 As DataTable
    2.         'Table1 = New DataTable("Contacts")
    3.  
    4.  
    5.         'creating a table named contacts
    6.         f1.DataGrid1.ReadOnly = True
    7.         'so that no changes are made to the data grid
    8.  
    9.         Dim Row1, Row2, Row3, Row4, Row5 As DataRow
    10.         'declaring five rows for the table
    11.         Try
    12.             Dim Name As DataColumn = New DataColumn("First Name")
    13.             'declaring a column named First Name
    14.             Name.DataType = System.Type.GetType("System.String")
    15.             'setting the datatype for the column
    16.             Table1.Columns.Add(Name)
    17.             'adding the column to table
    18.             Dim Last As DataColumn = New DataColumn("Last Name")
    19.             Last.DataType = System.Type.GetType("System.String")
    20.             Table1.Columns.Add(Last)
    21.             Dim Title As DataColumn = New DataColumn("Title")
    22.             Title.DataType = System.Type.GetType("System.String")
    23.             Table1.Columns.Add(Title)
    24.  
    25.             Dim phone As DataColumn = New DataColumn("Phone")
    26.             phone.DataType = System.Type.GetType("System.String")
    27.             Table1.Columns.Add(phone)
    28.  
    29.             Dim Email As DataColumn = New DataColumn("Email")
    30.             Email.DataType = System.Type.GetType("System.String")
    31.             Table1.Columns.Add(Email)
    32.  
    33.  
    34.  
    35.             Row1 = Table1.NewRow()
    36.             'declaring a new row
    37.             Row1.Item("First Name") = TextBox3.Text
    38.             'filling the row with values. Item property is used to set the field value.
    39.             Row1.Item("Last Name") = TextBox4.Text
    40.             'filling the row with values. adding Last Name
    41.             Row1.Item("Title") = TextBox2.Text
    42.             'filling the row with values. adding a title
    43.             Row1.Item("Phone") = TextBox5.Text
    44.             'filling the row with phone number
    45.             Row1.Item("Email") = TextBox6.Text
    46.  
    47.             'filling the row with email id
    48.             Table1.Rows.Add(Row1)
    49.  
    50.             f1.DataGrid1.Refresh()
    51.             'adding the completed row to the table
    52.         Catch
    53.         End Try
    54.         Dim ds As New DataSet
    55.         ds = New DataSet
    56.  
    57.         'creating a dataset
    58.         ds.Tables.Add(Table1)
    59.         'adding the table to dataset
    60.         f1.DataGrid1.SetDataBinding(ds, "Contacts")
    61.         'binding the table to datagrid
    62.  
    63.  
    64.  
    65.         ' for resizing the column width of datagrid
    66.  
    67.         'Clear the table styles for the datagrid
    68.         f1.DataGrid1.TableStyles.Clear()
    69.  
    70.         'Declare local variables
    71.         Dim tsTableStyle As New DataGridTableStyle
    72.         Dim tcTextCol As DataGridTextBoxColumn
    73.         Dim intCounter As Integer
    74.  
    75.         'Map the table style to the table in the dataset
    76.         tsTableStyle.MappingName = ds.Tables(0).TableName()
    77.  
    78.         'Add textbox column style for each column in the dataset
    79.         For intCounter = 0 To ds.Tables(0).Columns.Count() - 1
    80.  
    81.             'Reinstantiate the text box column and set mappings and attributes
    82.             tcTextCol = New DataGridTextBoxColumn
    83.             tcTextCol.MappingName = ds.Tables(0).Columns.Item(intCounter).ColumnName
    84.             tcTextCol.HeaderText = ds.Tables(0).Columns.Item(intCounter).ColumnName
    85.  
    86.             'Set column size based on name
    87.             Select Case tcTextCol.MappingName()
    88.                 Case "First Name"
    89.                     tcTextCol.Width() = 90
    90.                 Case "Last Name"
    91.                     tcTextCol.Width() = 90
    92.                 Case "Title"
    93.                     tcTextCol.Width() = 40
    94.                 Case "Phone"
    95.                     tcTextCol.Width() = 100
    96.                 Case "Email"
    97.                     tcTextCol.Width() = 180
    98.  
    99.  
    100.                 Case Else
    101.                     tcTextCol.Width() = 0
    102.             End Select
    103.  
    104.  
    105.  
    106.             'Add the column to the table style
    107.             tsTableStyle.GridColumnStyles.Add(tcTextCol)
    108.         Next
    109.         'Add the table styles to the datagrid
    110.         f1.DataGrid1.TableStyles.Add(tsTableStyle)
    111.  
    112.  
    113.         f1.Show()
    visual basic.net (2008)

    .net framework 3.5


  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    490

    Re: how to automatically add more rows in a datagrid on button click

    kindly help
    still stuck.

    cheers
    visual basic.net (2008)

    .net framework 3.5


  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to automatically add more rows in a datagrid on button click

    You still have to create a DataTable, but you only create one DataTable. You don't create a new one every time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    490

    Re: how to automatically add more rows in a datagrid on button click

    Quote Originally Posted by jmcilhinney
    You still have to create a DataTable, but you only create one DataTable. You don't create a new one every time.
    but i removed the code for the new datatable?
    visual basic.net (2008)

    .net framework 3.5


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width