Solved

VB 2008  - Assign a value to a DataGridView field

Posted on 2009-07-07
14
166 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
  • 8
  • 6
14 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now