VB 2008 - Assign a value to a DataGridView field

The DataGridView has a field, EvalDate, to which I want to assign a value (NOW).  The user can modify this value if necessary or desired.  I am attempting to assign it in the RowEnter event but that does not work.  See the code below.

How should this date be assigned?
Private Sub EvalItemsDataGridView_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles EvalItemsDataGridView.RowEnter
 
        Dim Now As DateTime = DateTime.Now
        EvalItemsDataGridView.Item("EvalDate", EvalItemsDataGridView.CurrentRow.Index).Value = Now.ToString("d")
 
End Sub

Open in new window

ljcorAsked:
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.

CodeCruiserCommented:
Is this grid databound? What object are you using for data? Is it datatable? You can set a value like

DataGridView1.Rows(0).Cells("ColumnName").Value = now.tostring
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
ljcorAuthor Commented:
Using your guideline I inserted this statement in RowEnter:
   EvalItemsDataGridView.Rows(0).Cells("EvalDate").Value = Now.ToString

I receive an error: Column EvalDate cannot be found.  

The Edit Columns property is shown below.
error.JPG
0
CodeCruiserCommented:
You get this error because the name of the column is DataGridViewTextBoxColumn8! Try using this name orelse try using an index ( i think it would be 6 for EvalDate).
0
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

ljcorAuthor Commented:
I forgot to answer:  the grid is bound from a datatable.
0
CodeCruiserCommented:
If the grid is bound to a datatable and you need to set the value of this cell for every row, you could loop through the table and set the value

For i as integer = 0 to dTable.Rows.Count
    dTable.Rows(i).Item(6) = Now.ToString
Next

Open in new window

0
ljcorAuthor Commented:
I'm sorry but I just don't understand.  EvalDate is the 7th displayed column in my grid.  Yet the properties window shows it as "Column 8" and the Index is 6.  

OK - I can see that the Index is based at 0 making the 7th column index = 6 (I guess.)  But why is the property name: DataGridViewTextBoxColumn8?  

And when I change the statement to your suggestion, it works perfectly.
   EvalItemsDataGridView.Rows(0).Cells("DataGridViewTextBoxColumn8").Value = Now.ToString

But I really don't understand this Column8 reference.
0
ljcorAuthor Commented:
I didn't have to reset the values for column 6 as I am just testing, trying to get it right.  And other things, too.
0
CodeCruiserCommented:
The EvalDate is the Caption of the column. The name is Column8 because these are autogenerated sequentially so may be you added this column after adding seven columns before it and then repositioned it.
0
ljcorAuthor Commented:
To carry this on a bit: The Key field for this record is uProjectID.  The value is generated as automatically sequenced by SqlServer.

I would like to also generate this in RowEnter but I can't figure out how to do that.  I presently do it in RowLeave but then it is possible to screw things up.  For instance, if the user enters a portion of the data in the row and then clicks on the Save icon.  No KeyID will have been generated at that point.

What do you suggest?
0
ljcorAuthor Commented:
The present code for uProjectID (which is Visible=False)  in RowLeave is:
        uItemID = (EvalItemsDataGridView.CurrentRow.Cells(8).Value())
        EvalBox0.Text = uItemID

I need the uItemID and EvalBox0 for other work.
0
CodeCruiserCommented:
As you said the uProjectID is autogenerated by SQL Server, why do you want to populate this value yourself?
0
ljcorAuthor Commented:
I don't mind at all that it is autopopulated but am concerned about when it gets populated.  I think it should be before other fields in the row receive data entry.  Am I wrong in this thinking?  What if only some of the fields are entered and the user hits the Save icon?
0
CodeCruiserCommented:
When the user hits the Save icon, the row would be saved to the SQL server with the key generated at thtat time. The SQL Server autogenerated fields get the autogenerated value when the insert statement is executed on the server. You should be fine without entering this value.
0
ljcorAuthor Commented:
OK, I lam learning.  I thought that was possibly the case but I had no sure knowledge.
Thanks once again for all your help.
0
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
Visual Basic.NET

From novice to tech pro — start learning today.