VB 2008 insert multiple selected records from a datagrid

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?

Thanks,

Michael
MKC06Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Priest04Commented:
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.
MKC06Author Commented:
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!
Michael

UPDATE
Priest04Commented:
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
        selectedRows.Add(row.Index)
    End If
Next

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. :)
Priest04Commented:
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
Next

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.