Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

How formatting a data in a dataGridView

$
0
0

I have a `DataGridView` in a `C#/WinForms` application.

I would like to input a date into a cell.

If the user input `01/01/2012`, it's ok, but if `01012012` is input, I have an Exception.

I'm able to verify the input using the `CellValidating` event.

Nevertheless, I would like to auto-format if the user inputs a date like `01012012`, and apparently, I need to do this in the CellValidated event.

Here is my code:

   
 private void dataGridView_BadgeService_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
        if (dataGridView_BadgeService.Columns[e.ColumnIndex].Name == "DateDebut" || dataGridView_BadgeService.Columns[e.ColumnIndex].Name == "DateFin")
        {
            String date = Convert.ToString(e.FormattedValue).Trim();
            if (date.Length > 0)
            {
                try
                {
                    DateTime _date;
                    if (DateTime.TryParse(date, out _date) == false)
                    {
                        if (DateTime.TryParseExact(date, "ddMMyyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out _date) == false)
                        {
                            MessageBox.Show("Merci de saisir une date, ou laissez cette zone vierge", "Action-Informatique", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            e.Cancel = true;
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("Merci de saisir une date, ou laissez cette zone vierge", "Action-Informatique", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    e.Cancel = true;
                }
            }
        }
    }
    private void dataGridView_BadgeService_CellValidated(object sender, DataGridViewCellEventArgs e)
    {
        if (dataGridView_BadgeService.Columns[e.ColumnIndex].Name == "DateDebut" || dataGridView_BadgeService.Columns[e.ColumnIndex].Name == "DateFin")
        {
            String date = Convert.ToString(dataGridView_BadgeService.Rows[e.RowIndex].Cells[e.ColumnIndex].Value).Trim();
            if (date.Length > 0)
            {
                try
                {
                    DateTime _date;
                    if (DateTime.TryParse(date, out _date) == true)
                    {
                        dataGridView_BadgeService.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = _date.ToShortDateString();
                    }
                    else
                    {
                        if (DateTime.TryParseExact(date, "ddMMyyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out _date) == true)
                        {
                            dataGridView_BadgeService.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = _date.ToShortDateString();
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("Merci de saisir une date, ou laissez cette zone vierge", "Action-Informatique", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }
    }

I don't know why, but if I input `01012012`, the `CellValidated` event does not fire. I have a `DataGridView Exception` about a bad format for the `DateTime`.

How can I auto-format my date in order to avoid this error?

Thanks a lot,
Nixeus


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>