none
Help in Comparing to Database RRS feed

  • Question

  • Hi i'm working on a small project which will need to determine if the the time is conflict to each other...

    for example i have a time of 10:00AM - 11:00AM and in the column there has 9:00AM - 12:00NN in the 1st row and 8:00AM to 11:00AM on the 2nd row

    how should i compare it so the 10:00AM - 11:00AM will not enter to the records and give an conflict message i used the LinQ Count() properties to count how many rows in a column but i don't know how to compare it.

     public void CellClick(object sender, DataGridViewCellEventArgs e)
            {
                if (e.RowIndex >= 0)
                {
                    DataGridViewRow dr = dataGridView1.Rows[e.RowIndex];
                    DataGridViewRow dr2 = (DataGridViewRow)dr.Clone();
                    for (int i = 0; i <= dr.Cells.Count - 1; i++)
                    {
                        dr2.Cells[i].Value = dr.Cells[i].Value;
                      
                    }
                    dataGridView2.Rows.Add(dr2);

    here are the codes the datagrid1 column 2 contains Start time and column 3 contains end time what i wanted before it will be added to another datagrid which is datagrid2 i want to check all column 2 of datagrid2 and column 3 of datagrid2 if the new added records will be conflict to each other

    for example the datagrid2 contains these records
    id no          Start time          End Time
    1                9:00am             12:00nn
    2                8:00am             9:00am
    3                12:00nn            1:00pm


    here is the datagrid1 that i wanted to add
    id no         Start time          End Time
    4               11:00am           12:00nn


    if i double click the row of datagrid1 it will first scan the all the start time and end time of the dtagrid2 so it will announce that these record of datagrid1 will not be added on datagrid2 because id no 4 of datagrid1 will conflict on id no 1 of datadrid2

    i used linq to retrieve all the records.

     var slist = from s in list.SectionTables
                            where s.SubjectID == listBox1.SelectedIndex + 1
                            select new
                            {
                                sectionName = s.SectionaName,
                                timeStart = s.StartTime.TimeOfDay,
                                endTime = s.EndTime.TimeOfDay
                            };
                dataGridView1.DataSource = slist;<input id="gwProxy" type="hidden"><!-- Session data--></input> <input id="jsProxy" onclick="jsCall();" type="hidden" />
    • Moved by Noam Ben-Ami - MSFT1 Friday, January 22, 2010 5:57 PM (From:ADO.NET Entity Framework and LINQ to Entities)
    Friday, January 22, 2010 10:45 AM

Answers

  • Hi newbie26,

    You can use DateTime(or TimeSpan)'s Compare method to compare two time values. As for your situation, please refer to the following codes:

            private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
            {            
                if (e.RowIndex >= 0)
                {
                    TimeSpan srtTime = (TimeSpan)dataGridView1.Rows[e.RowIndex].Cells[1].Value;
                    TimeSpan endTime = (TimeSpan)dataGridView1.Rows[e.RowIndex].Cells[2].Value;
                    for (int i = 0; i < dataGridView2.Rows.Count - 1; i++)
                    {
                        TimeSpan dg2StartTime = (TimeSpan)dataGridView2.Rows[i].Cells[1].Value;
                        TimeSpan dg2EndTime = (TimeSpan)dataGridView2.Rows[i].Cells[2].Value;
                        // change the following to your time compare logic.
                        if (TimeSpan.Compare(srtTime, dg2StartTime) >= 0 && TimeSpan.Compare(endTime, dg2EndTime) <= 0)
                        {
                            //MessageBox.Show("Time confict");
                            //return ;  
                        }
                    }
    
                    //DataGridViewRow dr = dataGridView1.Rows[e.RowIndex];
                    //DataGridViewRow dr2 = (DataGridViewRow)dr.Clone();
                    //for (int i = 0; i <= dr.Cells.Count - 1; i++)
                    //{
                    //    dr2.Cells[i].Value = dr.Cells[i].Value;
    
                    //}
                    //dataGridView2.Rows.Add(dr2); 
                    //....
                }
            }
    CompareTo() method is an alternative for Compare() method, it has the same function with Compare() method.
    The following link contains useful information about TimeSpan's Compare() and CompareTo() method.
    http://msdn.microsoft.com/en-us/library/system.timespan.compare.aspx
    http://msdn.microsoft.com/en-us/library/system.timespan.compareto.aspx


    Best regards,
    Alex LiangUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, January 25, 2010 8:50 AM
    Moderator