VB 2008 insert multiple selected records from a datagrid

Posted on 2008-11-07
Last Modified: 2012-06-21
Hi All,

I have a datagrid in a VB 2008 application I'm working on that has check boxes for selection.  I am connecting to a SQL 2000 server.  The Datagrid has only one column, I want to concatenate that column with a text box and create individual records and save them to the SQL server.  Can anyone show me how to construct the code for this task?


Question by:MKC06
    LVL 18

    Expert Comment

    Can you be more specific what you want to ahieve? is the control datagridview in question? and how would you like to "concatenate" data? and what is the query to insert data into database? You didnt provide much info.

    Author Comment

    Hi, yes, I will try to elaborate more on this.  I am using a DataGridView based on a Query I created with the designer.  The DataGrid Contains one column, UserName, I want to be able to select multiple names from the DataGrid, Concatenate them with a TextBox called Message, and the current time and date,  This works fine for a single record with a ComboBox, I use a parameter update query.  I was thinking I could do something similar with the multiple records but I get lost constructing the loops and so forth.

    I am running this code with a paramater query to create a single record;
    MessageLogTableAdapter.InsertUserMessage(Me.ComboBox1.Text, Me.TextBox2.Text, rGetUserName())

    The parameter insert query looks like this;
    INSERT INTO dbo.MessageLog
                          (UserID, WorkstationID, Department, Floor, Facility, DateNTime, Message, SentBy)
    VALUES     (?, NULL, NULL, NULL, NULL, { fn NOW() },?,?)

    I think I need to do something simular for multiple records from a DataGrid.

    Thanks again!

    LVL 18

    Expert Comment

    Ok, its a bit more clear. So you have 2 columns (not 1) in a datagridview, one is a checkbox, and another is a textbox column. Also, never call it datagrid, since there is a control called datagrid, and it has nothing to do with datagridview, so it may confuse someone that is trying to help you.

    If I got you, a user makes a selection of rows, and press a button. To get the indexes of selected rows, you can use the following code:

    Dim selectedRows As New List(Of Integer)()

    For Each row As DataGridViewRow In dataGridView1.Rows
        If Convert.ToBoolean(row.Cells(0).Value) = True Then ' replace 0 with the column index of check box columns
            ' this row is selected, so we are taking its index
            ' we could also call the method to insert record, if we wanted
        End If

    now we have a list of indexes of selected rows. This was just an example how to get the selected rows. We could avoid getting indexes and do an insert in the foreach loop, but I am uncertain what exactly do you want to concatenate. Just to clarify, you wish to do this?

    Dim result As string = dgv(1, selectedRowIndex).Value.ToString() + ";" + txtMessage.Text + ";" + DateTime.Now.ToString()

    or you want to concatenate all names from datagridview, and then to add message and current date and time?

    Also, is semi columns a sign that is used to concatenate strings?

    I guess I have given you ideas how to go further. You know the best what needs to be done. :)
    LVL 18

    Accepted Solution

    just to clarify the above line when I concatenate strings, it should be done in a loop
    For Each selectedRowIndex As Integer In selectedRows
        Dim result As string = dgv(1, selectedRowIndex).Value.ToString() + ";" + txtMessage.Text + ";" + DateTime.Now.ToString()
        ' now do what you want with the concatenated string

    Open in new window


    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now