Click here to Skip to main content
15,886,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends...

My problem is, I am not able to edit date column in datagridview

I am using access as DB in which there is field with datatype DATETIME named as DOB
datetime format is mm/dd/yyyy.
In front end i am displaying the date column as
C#
dataGridView1.Columns["DOB"].DefaultCellStyle.Format = "dd/MM/yyyy";

When i try to edit date column its throwing an error.
How can i overcome this situation.
I am using dataset and dataadapater to fetch the values from DB.
For saving i am using adapter.Update method

Error Details
Datagridview Default Error Dialog
System.FormatExecption: String was not recognized ass valid DateTime.
......
.....
To Replace this default dialog please handle the DataError event

Updated Question

Code for fetching data from DB
C#
dataGridView1.DataSource = dc.dsreturn_family("Select ID,FullName,Mobile,DOB,Gender,Status from Family where Hno=" + hnumber).Tables[0];
                      dataGridView1.AutoResizeColumns();
                      dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
                      dataGridView1.DefaultCellStyle.Font = new Font("Maiandra GD", 12);
                      dataGridView1.DefaultCellStyle.ForeColor = Color.Black;
                      dataGridView1.Columns[0].Visible = false;
                      dataGridView1.Columns["DOB"].ValueType = typeof(DateTime);
                      dataGridView1.Columns["DOB"].DefaultCellStyle.Format = "dd/MM/yyyy";


Error Comes.
When i try to edit DOB column.
Posted
Updated 18-Feb-15 3:52am
v4
Comments
That means the format, in which you are editing is wrong.
jinesh sam 18-Feb-15 6:41am    
yes you are correct... i need to edit date column which is of dd/MM/yyyy format
Richard Deeming 18-Feb-15 10:01am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

try this
DateTime dt=DateTime.ParseExact(stringdate, "MM/dd/yyyy", CultureInfo.InvariantCulture)

Now Pass this dt in update statement .
 
Share this answer
 
Comments
jinesh sam 18-Feb-15 6:43am    
@Animesh Datta... Can you explain little more?
Check the column ValueType property. If you want to do it programatically, you can do something like this:

C#
dataGridView1.Columns["DOB"].ValueType = typeof(DateTime);


Edit

I now think it's a problem of culture. Try adding this before setting DefaultCellStyle, if in your culture dd/MM/yyyy is the default date format:

C#
dataGridView1.Columns["DOB"].DefaultCellStyle.FormatProvider = CultureInfo.CurrentCulture;


If in your culture is not the habitual date format,try this for example:

XML
dataGridView1.Columns["DOB"].DefaultCellStyle.FormatProvider = CultureInfo.CreateSpecificCulture("es-ES");
 
Share this answer
 
v4
Comments
jinesh sam 18-Feb-15 6:39am    
Hi @Pikoh...Actually where should i place this code. I am getting error while i am editing date column. i am able to display date in dd/MM/yyyy format but the problem is when i try to make a change in date.
Pikoh 18-Feb-15 6:42am    
Try adding it just before binding the data to the datagrid.
jinesh sam 18-Feb-15 6:49am    
Before binding there will be no columns so i am getting an error "object reference not set to an instance of object"
jinesh sam 18-Feb-15 6:52am    
dataGridView1.DataSource = dc.dsreturn_family("Select ID,FullName,Mobile,DOB,Gender,Status from Family where Hno=" + hnumber).Tables[0];
dataGridView1.AutoResizeColumns();
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
dataGridView1.DefaultCellStyle.Font = new Font("Maiandra GD", 12);
dataGridView1.DefaultCellStyle.ForeColor = Color.Black;
dataGridView1.Columns[0].Visible = false;
dataGridView1.Columns["DOB"].ValueType = typeof(DateTime);
dataGridView1.Columns["DOB"].DefaultCellStyle.Format = "dd/MM/yyyy";
This is the code i am using to populate gridviw
Pikoh 18-Feb-15 9:34am    
First,to add code it's better you use "Improve Question" and add it to the original question.
That code seems right to me. When does the error rise? When you start editing the field or when you save? if so,improve question and show us the Update code.
Hi,

using System.Globalization;//namespace
DateTime From = DateTime.ParseExact(txtFrom.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);


Ref : What does CultureInfo.InvariantCulture mean
 
Share this answer
 
v3
Comments
jinesh sam 18-Feb-15 6:55am    
Hi @Rajeesh...Actually where should i place this code. I am getting error while i am editing date column. i am able to display date in dd/MM/yyyy format but the problem is when i try to make a change in date.
[no name] 18-Feb-15 6:57am    
u wanted to display ur date format dd/mm/yyyy ? then u change my code part as mm/dd/yyyy as dd/mm/yyyy
jinesh sam 18-Feb-15 7:00am    
S i want to display date in dd/mm/yyyy format.
[no name] 18-Feb-15 7:01am    
k then change the code as DateTime From = DateTime.ParseExact(txtFrom.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);
jinesh sam 18-Feb-15 7:06am    
Rajeesh...as i said earlier i am able to display date in required format.but the problem is while i try to make changes in that column.Please refer "pikoh" comments.in there i have placed code for popluating grid view

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900