[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

VB 2008  - Assign a value to a DataGridView field

Posted on 2009-07-07
14
Medium Priority
?
187 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:ljcor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
14 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 24793800
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
 

Author Comment

by:ljcor
ID: 24793885
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24793904
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ljcor
ID: 24793910
I forgot to answer:  the grid is bound from a datatable.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24793924
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
 

Author Comment

by:ljcor
ID: 24794024
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
 

Author Comment

by:ljcor
ID: 24794036
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24794287
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
 

Author Comment

by:ljcor
ID: 24794344
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
 

Author Comment

by:ljcor
ID: 24794365
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24794678
As you said the uProjectID is autogenerated by SQL Server, why do you want to populate this value yourself?
0
 

Author Comment

by:ljcor
ID: 24794725
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24794736
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
 

Author Closing Comment

by:ljcor
ID: 31618562
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

649 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