[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB 2008 insert multiple selected records from a datagrid

Posted on 2008-11-07
4
Medium Priority
?
1,301 Views
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?

Thanks,

Michael
0
Comment
Question by:MKC06
  • 3
4 Comments
 
LVL 18

Expert Comment

by:Priest04
ID: 22911589
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.
0
 

Author Comment

by:MKC06
ID: 22913833
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
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22915983
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. :)
0
 
LVL 18

Accepted Solution

by:
Priest04 earned 2000 total points
ID: 22915987
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

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

834 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