Click here to Skip to main content
13,191,400 members (70,901 online)
Rate this:
Please Sign up or sign in to vote.
using a datagridview to add data to a table. So i want to make everytime someone adds a new record in the datatable, a cell will receive a datetime value. So i did this:

DataGridView.CurrentRow.Cells[dataGridViewTextBoxColumn].Value = DateTime.Now;

the problem is that this gives an error and i don't know where to change. Can anyone give me a hint?
Posted 5-Jan-13 7:40am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


I test you code it execute without error.
Please attend to bellow point:
1)This code is OK.
dataGridView1.CurrentRow.Cells[YourColumnNumber].Value = DateTime.Now;

2)The first column number is zero, therefore if you want add date to third column you must use from 2 az YourColumnNumber

3)You can use from upper code in Button_Click

I hope it's helpful
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

The reason for the compiler error is you have used the dataGridViewColumn object to access the cell which accepts the Column index or name. Change your code like
DataGridView.CurrentRow.Cells[columnIndex].Value = DateTime.Now; or
DataGridView.CurrentRow.Cells[columnName].Value = DateTime.Now; 
where columnIndex/columnName is the number/Name that represents your column.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Just want to thank you Both. I was using the column name. But after reading your help, i realized it's simpler to use the index. But the error persisted. Left the code for a while, when came back i saw where i was wrong. It was wrong index. The datetime valu should go to [4] and i was giving to index [5] that is a string. The error message was pretty clear, but sometimes the brain sees everything but that. Thank you alot guys.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.171017.1 | Last Updated 6 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100