Click here to Skip to main content
Rate this: bad
good
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 8:40am
arkarna171
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi
 
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
  Permalink  
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 515
1 Maciej Los 340
2 Richard MacCutchan 265
3 BillWoodruff 225
4 Mathew Soji 155
0 OriginalGriff 8,804
1 Sergey Alexandrovich Kryukov 7,457
2 DamithSL 5,689
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411028.1 | Last Updated 6 Jan 2013
Copyright © CodeProject, 1999-2014
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