locked
DataGridView - Just an INSERT into a table (multiple rows). VB Windows Form App RRS feed

  • Question

  • I have a Windows application form.  Directly on the form I have a lable ("Customer Names"), a ComboBox (pulling actually names from a database) , and a Buton to add a customer to the database if i choose.  Additionally I have ControlTab(3 tabs) that has "fields" and its own Buttons to insert data into a different table.  The first 2 tabs have textBoxes, which are completely functional, but the 3rd tab has a "DataGridView" (so i thought i needed but from hearing you guys i was wrong...?). 

    Am I using the wrong tool for the 3rd tab?  What i am trying to do in the 3rd tab is to have a "editable grid" like structure of 6-7 columns with a maximum row height of 60.  In this grid a person will enter data for these fields and click a button to insert data into columns in SQl dataBase (i.e. Lot# (LotNumber), File(FileName), Record Count(Quantity), Starts#(SequenceMinimum), End#(SequenceMaximum), Date(OrderData), and Form Code(ProductionType))

    • Edited by Major MIS Friday, July 23, 2010 6:05 PM
    Thursday, July 22, 2010 9:03 PM

Answers

  • Hi Major,

    We can use a datagridview to input data and store it to database, here is a sample:

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'create connection
            Dim conn As SqlConnection = New SqlConnection()
            conn.Open()

            Dim comm As SqlCommand = New SqlCommand()
            comm.Connection = conn

            'insert data to sql database row by row
            Dim name, old As String
            For i As Integer = 0 To Me.DataGridView1.Rows.Count
                name = Me.DataGridView1.Rows(i).Cells(0).ToString()
                old = Me.DataGridView1.Rows(i).Cells(1).ToString()

                comm.CommandText = "insert into mytable(name,old) values('" & name & "','" & old & "')"
                comm.ExecuteNonQuery()
            Next

            conn.Close()

        End Sub

    Above sample is just a demo, I don't test it. you have to change connection string and test it.

    If this is not the case, please let me know.

    Best regards,
    Guang-Ming Bian - MSFT
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    • Marked as answer by Major MIS Monday, July 26, 2010 2:54 PM
    Monday, July 26, 2010 6:55 AM
    Moderator

All replies

  • Hi Major,

    I am confused about your description, it doesn't make sense. Could you explain your question detailedly?

     

     

    Best regards,
    Guang-Ming Bian - MSFT
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    Friday, July 23, 2010 6:24 AM
    Moderator
  • Hello,

    Mostly persons put DataGridView in the header of a question while it is DataGrid

    A Web application has no DataGridView it has a DataGrid and a GridView

    However, the most common mistake which is made with ASP.Net is that persons forget to put

    DG.DataBind as last method in their code.

     


    Success
    Cor
    Friday, July 23, 2010 6:52 AM
  • Maybe if i can do a print screen along with a more detailed description it would be eaiser to understand.  How do you put a print screen in this window anyways?  For now here is a bit more of the description...

    I have a Windows application form.  Directly on the form I have a lable ("Customer Names"), a ComboBox (pulling actually names from a database) , and a Buton to add a customer to the database if i choose.  Additionally I have ControlTab(3 tabs) that has "fields" and its own Buttons to insert data into a different table.  The first 2 tabs have textBoxes, which are completely functional, but the 3rd tab has a "DataGridView" (so i thought i needed but from hearing you guys i was wrong...?). 

    Am I using the wrong tool for the 3rd tab?  What i am trying to do in the 3rd tab is to have a "editable grid" like structure of 6-7 columns with a maximum row height of 60.  In this grid a person will enter data for these fields and click a button to insert data into columns in SQl dataBase (i.e. Lot# (LotNumber), File(FileName), Record Count(Quantity), Starts#(SequenceMinimum), End#(SequenceMaximum), Date(OrderData), and Form Code(ProductionType))

     

    Friday, July 23, 2010 1:21 PM
  • Additionally when i click the button it should loop through each row in the grid and each data row gets inserted into a table.
    Friday, July 23, 2010 1:23 PM
  • Cor:

    Youre completely right.  Its a hard app and not a webapp.  Sorry. I just caught that.  I meant to put WindowsAPP

    Friday, July 23, 2010 1:38 PM
  • Hi Major,

    We can use a datagridview to input data and store it to database, here is a sample:

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'create connection
            Dim conn As SqlConnection = New SqlConnection()
            conn.Open()

            Dim comm As SqlCommand = New SqlCommand()
            comm.Connection = conn

            'insert data to sql database row by row
            Dim name, old As String
            For i As Integer = 0 To Me.DataGridView1.Rows.Count
                name = Me.DataGridView1.Rows(i).Cells(0).ToString()
                old = Me.DataGridView1.Rows(i).Cells(1).ToString()

                comm.CommandText = "insert into mytable(name,old) values('" & name & "','" & old & "')"
                comm.ExecuteNonQuery()
            Next

            conn.Close()

        End Sub

    Above sample is just a demo, I don't test it. you have to change connection string and test it.

    If this is not the case, please let me know.

    Best regards,
    Guang-Ming Bian - MSFT
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    • Marked as answer by Major MIS Monday, July 26, 2010 2:54 PM
    Monday, July 26, 2010 6:55 AM
    Moderator