Validate Time in DataGridView cell in Windows Application

Last Reply 2 months ago By pandeyism

Posted 2 months ago

Hi..

I want to validate datagridviewcell which contains data in hh:mm:ss in 12 hours as well as in 24 hour format ,how i can validate this..

my date is in followinf format:

inTime       outTime

12:36:60    11:22:15

1:25:26      15:02:03

Sir,Now what I want is if user enter only hours,or only minutes or only second,it should validate,morevever user cannot type 89 in case of minutes or hours,user should put colon,not semicolon in time format.

Hope you are getting me

Posted 2 months ago Modified on 2 months ago

Hi priyajsr,

Please take refernce to the below code and correct your code.

HTML

Namespaces

C#

using System.Data;
using System.Text.RegularExpressions;

Code

C#

public Form1()
{
    InitializeComponent();
    dataGridView1.AutoGenerateColumns = true;
    dataGridView1.AllowUserToAddRows = false;
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Id", typeof(int)),
    new DataColumn("Time", typeof(string)) });
    dt.Rows.Add(1, 50);
    dt.Rows.Add(2, 60);
    dt.Rows.Add(3, 62);
    dt.Rows.Add(4, 90);
    dataGridView1.DataSource = dt;
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        int time = Convert.ToInt32(row.Cells[1].Value);
        var timeSpan = TimeSpan.FromMinutes(time);
        int hh = timeSpan.Hours;
        int mm = timeSpan.Minutes;
        int ss = timeSpan.Seconds;
        string hours = hh.ToString().Length == 1 ? hh.ToString().PadLeft(2, '0') : hh.ToString();
        string minutes = mm.ToString().Length == 1 ? mm.ToString().PadLeft(2, '0') : mm.ToString();
        string seconds = ss.ToString().Length == 1 ? ss.ToString().PadLeft(2, '0') : ss.ToString();
        row.Cells[1].Value = string.Format("{0}:{1}:{2}", hours, minutes, seconds);
    }
}

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    string time = (sender as DataGridView).Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
    string reg = "(([0-1][0-9])|([2][0-3])):([0-5][0-9]):([0-5][0-9])";
    Regex regex = new Regex(reg);
    bool isValid = regex.IsMatch(time);
    if (!isValid)
    {
        MessageBox.Show("please enter Valid Time");
    }
}

VB.Net

Public Sub New()
    InitializeComponent()
    dataGridView1.AutoGenerateColumns = True
    dataGridView1.AllowUserToAddRows = False
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Id", GetType(Integer)), New DataColumn("Time", GetType(String))})
    dt.Rows.Add(1, 50)
    dt.Rows.Add(2, 60)
    dt.Rows.Add(3, 62)
    dt.Rows.Add(4, 90)
    dataGridView1.DataSource = dt

    For Each row As DataGridViewRow In dataGridView1.Rows
        Dim time As Integer = Convert.ToInt32(row.Cells(1).Value)
        Dim timeSpan As TimeSpan = timeSpan.FromMinutes(time)
        Dim hh As Integer = timeSpan.Hours
        Dim mm As Integer = timeSpan.Minutes
        Dim ss As Integer = timeSpan.Seconds
        Dim hours As String = If(hh.ToString().Length = 1, hh.ToString().PadLeft(2, "0"c), hh.ToString())
        Dim minutes As String = If(mm.ToString().Length = 1, mm.ToString().PadLeft(2, "0"c), mm.ToString())
        Dim seconds As String = If(ss.ToString().Length = 1, ss.ToString().PadLeft(2, "0"c), ss.ToString())
        row.Cells(1).Value = String.Format("{0}:{1}:{2}", hours, minutes, seconds)
    Next
End Sub

Private Sub dataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
    Dim time As String = (TryCast(sender, DataGridView)).Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString()
    Dim reg As String = "(([0-1][0-9])|([2][0-3])):([0-5][0-9]):([0-5][0-9])"
    Dim regex As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(reg)
    Dim isValid As Boolean = regex.IsMatch(time)
    If Not isValid Then
        MessageBox.Show("please enter Valid Time")
    End If
End Sub

Screenshot