Results 1 to 6 of 6

Thread: [2005] datagridview just insert 1row: s

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2007
    Location
    Lisboa,Portugal
    Posts
    71

    [2005] datagridview just insert 1row: s

    i have 2 textboxes,1 gridview, 1button

    i put some text into the textboxes then i press the button, i can get 1 row....but when i repeat the proccess replaces the last row whith new values...always just have 1 row in my grid...

    i want more then 1 row, if i press the button 5 times e want 5 rows

    any help???

    sorry for my english


    inside the button i have this code

    Code:
    'Criando o Dataset 
            Dim dsFornecedor As New DataSet()
    
            'Criando a tabela 
            Dim tblFornecedores As New DataTable()
            dsFornecedor.Tables.Add(tblFornecedores)
    
    
            'Estrutura do DataTable 
    
            tblFornecedores.Columns.Add("CodFornecedor")
            tblFornecedores.Columns.Add("NomeFornecedor")
           
    
            'Criando Registros 
            Dim row As DataRow = tblFornecedores.NewRow()
    
            row("CodFornecedor") = TextBox1.Text
            row("NomeFornecedor") = TextBox2.Text
    
            tblFornecedores.Rows.Add(row)
    
    
            DataGridView1.DataSource = dsFornecedor.Tables(0)
    Did a reply help answer your questions? Please rate good posts!

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

    Re: [2005] datagridview just insert 1row: s

    That's because you are creating a completely new DataSet and DataTable each time. You are discarding the one that contains the previous row so you lose all the old data. You should creating one DataTable only and binding that to the grid in the beginning. You then add a new row to THAT DataTable each 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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2007
    Location
    Lisboa,Portugal
    Posts
    71

    Re: [2005] datagridview just insert 1row: s

    Please can you show me some example?? im newbie i dont now how to what you said...thanksss
    Did a reply help answer your questions? Please rate good posts!

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

    Re: [2005] datagridview just insert 1row: s

    Yes you do, because you're already doing it. You're already creating a DataTable and adding a row to it, and that's all you need to do. The change you need to make is to take the part that creates the DataTable OUT of the code that gets called over and over again and put it somewhere that it will only be executed once, like the load event handler of the form. That way you create one table and one table only, then each time you create a new row you add it to that same table.
    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

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2007
    Location
    Lisboa,Portugal
    Posts
    71

    Re: [2005] datagridview just insert 1row: s

    thanksss ... i done what you said and it worksssssss .. now i have 5...30...50... rowsss
    Did a reply help answer your questions? Please rate good posts!

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

    Re: [2005] datagridview just insert 1row: s

    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

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