Insert value into a DataGridView column

Hi,
I'm working w/ VB.Net 2005.
I need to find a way to insert a value into an entire column in a datagridview. The value is stored in a variable and it is the Julian date.  I've been trying to use something like the line below, but I haven't got it to work yet. How can I accomplish this?
DataGridView1.Rows(e.RowIndex).Cells("Wave").Value = X
coperations07Asked:
Who is Participating?
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.

Jorge PaulinoIT Pro/DeveloperCommented:
Try this:

For Each row As DataGridViewrow In DataGridView1.Rows
  row.Cells("Wave").Value = X
Next
0
coperations07Author Commented:
Thanks!  this is working except the first record doesn't get a value. Why would this happen?
0
Jorge PaulinoIT Pro/DeveloperCommented:
Can you show what you have now ?
0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

coperations07Author Commented:
This is the code I have now. It's in a click event.
        Dim x, sJulian As String
        sJulian = Now.DayOfYear.ToString.PadLeft(3, "0")
        x = InputBox("Name the Wave", "Wave Name", sJulian)
        'set adapter to tbl_Current_Wave. Store DGV data in tSorter.
        Me.Validate()
        Dim cn As New OleDbConnection(connectionString)
        cn.Open()
        Dim tSorter As DataTable = Me.dgResults.DataSource
        mAdapter.SelectCommand = New OleDb.OleDbCommand("SELECT * FROM tbl_Current_Wave", New OleDb.OleDbConnection(connectionString))
        Dim cb As New OleDb.OleDbCommandBuilder(mAdapter)
 
        For Each row As DataGridViewRow In dgResults.Rows
            row.Cells("Wave").Value = x
        Next
 
        'Update Database
        Dim rowsAffected As Integer = mAdapter.Update(tSorter)
        MessageBox.Show("Rows affected: " & rowsAffected)
        cn.Close()

Open in new window

0
Jorge PaulinoIT Pro/DeveloperCommented:
Try to make the loop after the update. This way:

        Dim x, sJulian As String
        sJulian = Now.DayOfYear.ToString.PadLeft(3, "0")
        x = InputBox("Name the Wave", "Wave Name", sJulian)
        'set adapter to tbl_Current_Wave. Store DGV data in tSorter.
        Me.Validate()
        Dim cn As New OleDbConnection(connectionString)
        cn.Open()
        dgResults.SuspendLayout()
        Dim tSorter As DataTable = Me.dgResults.DataSource
        mAdapter.SelectCommand = New OleDb.OleDbCommand("SELECT * FROM tbl_Current_Wave", New OleDb.OleDbConnection(connectionString))
        Dim cb As New OleDb.OleDbCommandBuilder(mAdapter)
 
        'Update Database
        Dim rowsAffected As Integer = mAdapter.Update(tSorter)
 
        For Each row As DataGridViewRow In dgResults.Rows
            row.Cells("Wave").Value = x
        Next
        dgResults.ResumeLayout()
        MessageBox.Show("Rows affected: " & rowsAffected)
        cn.Close()

Open in new window

0
coperations07Author Commented:
It shows in the grid that all the records get the value.  This way doesn't put the records for "Wave" into the database, since the update is before the loop. I tried the other way again, but it still doesn't give the first record a value.
0
coperations07Author Commented:
I did a little trouble shooting on this and now I'm trying to figure out what the results mean. I was only filling the DGV w/ 4 rows for testing.
Test 1:
I entered a value in each row of the "Wave" column. Then I hit the accept button and It put all 4 rows into the database w/ the correct value(Julian Date) for each, which is the result I'm going for.
Test 2: I clicked on each cell ,from top to bottom, of the "Waves" column. result In the database was that the first 3 rows were set w/ the correct Julian Date, but the bottom row was empty for the "Waves" column.
Test 3: I clicked the 3rd cell. result was the 3rd row's "Wave" column was empty and the others had the correct value.
0
Jorge PaulinoIT Pro/DeveloperCommented:
Ok, give me some minutes to test!
0
Jorge PaulinoIT Pro/DeveloperCommented:
The last row is to insert the a new row you can jump that:

        For Each row As DataGridViewRow In dgResults.Rows
            If row.Cells(OtherColumnWithValues).Value <> "" Then
                row.Cells("Wave").Value = 1
            End If
        Next
0
Jorge PaulinoIT Pro/DeveloperCommented:
My test work fine! Can you upload the project ? You can do that on www.ee-stuff.com
0
coperations07Author Commented:
I tried the code and I'm still getting the same results. It shows up fine in the datagridveiw, but just leaves the first cell blank in the database. Its really weird because it goes through the loop to give the column its value before it updates to the database, so I can't see any reason why this isn't working.
Which file do I upload on the ee-stuff site?
0
Jorge PaulinoIT Pro/DeveloperCommented:
If it's not a problem to you all (project + db). If it's not possible that you can make a demo (with the problem)
0
Jorge PaulinoIT Pro/DeveloperCommented:
You have to zip it and mention this question ID (23158246)
0
coperations07Author Commented:
Okay. I've uploaded the project w/ the database zipped in.
0
Jorge PaulinoIT Pro/DeveloperCommented:
Try this way:
Dim x, sJulian As String
        sJulian = Now.DayOfYear.ToString.PadLeft(3, "0")
        x = InputBox("Name the Wave", "Wave Name", sJulian)
        'set adapter to tbl_Current_Wave. Store DGV data in tSorter.
        Me.Validate()
        Dim cn As New OleDbConnection(connectionString)
        cn.Open()
        Dim tSorter As DataTable = Me.dgResults.DataSource
        mAdapter.SelectCommand = New OleDb.OleDbCommand("SELECT * FROM tbl_Current_Wave", New OleDb.OleDbConnection(connectionString))
        Dim cb As New OleDb.OleDbCommandBuilder(mAdapter)
 
        Dim dv As DataView = tSorter.DefaultView
        For y As Integer = 0 To dv.Table.Rows.Count - 1
           dv.Table.Rows(y).Item("Wave") = x
        Next y
 
        'Update Database
        Dim rowsAffected As Integer = mAdapter.Update(tSorter)
 
        MessageBox.Show("Rows affected: " & rowsAffected)
        cn.Close()

Open in new window

0

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
.NET Programming

From novice to tech pro — start learning today.