locked
DateTime Updation in DataGridView RRS feed

  • Question

  •  thanks in advance!

    Actually, I show the "dd/MM/yyyy" formate in dataGridView1_CellFormatting event.
    when user change it, it shows error due to String not in DataTime formate.

    I convert it in cellValidation event to "mm/dd/yyyy" formate, but still getting error.

    How i can save the value in database & show the date in "dd/MM/yyyy" formate?
    Rakesh Jha
    • Moved by jack 321 Thursday, June 5, 2008 8:03 AM off topic for C# general
    Tuesday, June 3, 2008 1:18 PM

Answers

  • I got the solution

    protected static IFormatProvider culture = new CultureInfo("fr-FR", true);

    private void dataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e)

    {

    if (e.ColumnIndex == 5)

    {

    if (e.DesiredType.Name.ToString() == "DateTime"){

    e.Value = DateTime.Parse(e.Value.ToString(), culture, DateTimeStyles.NoCurrentDateDefault);

    e.ParsingApplied = true;

    }

    }

    }


    Rakesh Jha
    • Marked as answer by Rakesh Jha Friday, June 13, 2008 8:41 AM
    Friday, June 13, 2008 8:41 AM

All replies

  • Remove the event handlers altogether.  Edit the properties for the appropriate column instead.  The DefaultCellStyle property contains the style settings for the cells of the column.  Set the Format property of this type to the format string you want to use.  The appropriate DateTime field should be bound to your cell already.  The grid will then use the real value for editing but will display the value using the string you specified.

    Michael Taylor - 6/3/08
    http://p3net.mvps.org
    Tuesday, June 3, 2008 1:30 PM
  • in dataGridView1_CellFormatting event, set 

    e.Value = "your date formatted string...";
    e.FormattingApplied = true;


    Bob - www.crowcoder.com
    Tuesday, June 3, 2008 1:33 PM
  • I already used this method. What happens when cell show the value 'dd/MM/YYYY' when updating it,    it shows error of String formate is invalid datetime.
    Rakesh Jha
    Wednesday, June 4, 2008 7:34 AM
  • through below code, error doesn't show, but new problems occured.

    private
    void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {

    if (e.ColumnIndex == 5)

    {

        dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = DateTime.Parse(e.FormattedValue.ToString(), culture, DateTimeStyles.NoCurrentDateDefault).ToString();

    }

    }

    private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)

    {

    e.Cancel = false;

    }


    when i changed the value for example,
        "28/04/2005" to "22/04/2005" its works but when changing
        "04/05/2005" to "03/05/2005" its cell swap the day & month value by default, i.e. it shows "05/03/2005", Can any one help?

    thanks for any reply


    Rakesh Jha
    Thursday, June 5, 2008 11:38 AM
  • I got the solution

    protected static IFormatProvider culture = new CultureInfo("fr-FR", true);

    private void dataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e)

    {

    if (e.ColumnIndex == 5)

    {

    if (e.DesiredType.Name.ToString() == "DateTime"){

    e.Value = DateTime.Parse(e.Value.ToString(), culture, DateTimeStyles.NoCurrentDateDefault);

    e.ParsingApplied = true;

    }

    }

    }


    Rakesh Jha
    • Marked as answer by Rakesh Jha Friday, June 13, 2008 8:41 AM
    Friday, June 13, 2008 8:41 AM