Solved

VB 2008  - Assign a value to a DataGridView field

Posted on 2009-07-07
14
172 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

770 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