Ted Philip

Ted Philip

  • NA
  • 1
  • 999

C# looping through gridview row

Jun 11 2015 7:22 AM

 Hi again

I scratching my head yet again. Hope you can help.

I have a gridview containing list of equipment. when a person choose a time to book equipment, the system should check the time against the database and toggle the visibility of two controls (checkbox and label). if time clash of overlap is found, then the label should be displayed and checkbox hidden. if not then the other way around is true.

Here is the problem; I have it working as it should, but if time overlap for one equipment is found, then all labels for all equipment is displayed, and checkbox is hidden, if not all checkbox is displayed and label hidden.

I would like to know how I can get it to perform the operation only for rows that have the time overlap or clash, and make other equipment available.

 
Here is what the controls look like;
 
<ItemTemplate>
                                    <asp:CheckBox ID="CheckBox1" runat="server"   />
                                    <asp:Label ID="Label1" runat="server" ForeColor="Red" Text="Unavailable" ></asp:Label>
</ItemTemplate>

 Here is the full code behind. 


protected void CheckAvailability_Click(object sender, EventArgs e)
        {
 
            DateTime startDateTime = DateTime.ParseExact(StartDate.Text, "yyyy-MM-dd", CultureInfo.InvariantCulture);
            TimeSpan starttime = TimeSpan.ParseExact(StartTime.SelectedValue, "h\\:mm", CultureInfo.InvariantCulture);
 

            DateTime endDateTime = DateTime.ParseExact(FinishDate.Text, "yyyy-MM-dd", CultureInfo.InvariantCulture);
            TimeSpan endtime = TimeSpan.ParseExact(FinishTime.SelectedValue, "h\\:mm", CultureInfo.InvariantCulture);
 
            //Checking asset is available for booking:

 
            if ((endDateTime > startDateTime) || (endDateTime == startDateTime))
 
                GridView1.Visible = true;
 
            else
                GridView1.Visible = false;
 
            if ((startDateTime == DateTime.Now.Date) || (endDateTime == DateTime.Now.Date))
 
                SameDayBooking.Visible = true;
            else
                SameDayBooking.Visible = false;
 

            if (((FinishTime.SelectedIndex < StartTime.SelectedIndex) || (FinishTime.SelectedIndex == StartTime.SelectedIndex))
                 && ((endDateTime < startDateTime) || (endDateTime == startDateTime)))
            {
                timeError.Visible = true;
                GridView1.Visible = false;
 

            }
            else
            {
                timeError.Visible = false;
                GridView1.Visible = true;
            }
 

            
                string cs = System.Configuration.ConfigurationManager.ConnectionStrings["AssetBookingSystemConnectionString"].ConnectionString;
 

 
                SqlConnection con = new SqlConnection(cs);
                SqlCommand cmd = new SqlCommand();
                SqlDataReader reader;
                cmd.CommandText = "SELECT StartDate, EndDate, StartTime, EndTime FROM tblBooking";
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
 
                con.Open();
                reader = cmd.ExecuteReader();
 

                DataTable table = new DataTable();
                table.Columns.Add("SDate");
                table.Columns.Add("FDate");
                table.Columns.Add("STime");
                table.Columns.Add("ETime");
 

 
                while (reader.Read())
                {
                    DataRow dataRow = table.NewRow();
 
                    DateTime startDate = Convert.ToDateTime(reader["StartDate"]);
                    DateTime endDate = Convert.ToDateTime(reader["EndDate"]);
 
                    TimeSpan startTime = (TimeSpan)reader["StartTime"];
                    TimeSpan endTime = (TimeSpan)reader["EndTime"];
 

 

                    dataRow["SDate"] = startDate;
                    dataRow["FDate"] = endDate;
                    dataRow["STime"] = startTime;
                    dataRow["ETime"] = endTime;
                    table.Rows.Add(dataRow);
 
                    foreach (GridViewRow gridViewRow in GridView1.Rows)
                    {
                        if (((startDate <= startDateTime && startDateTime <= endDate)
                           || (startDate <= endDateTime && endDateTime <= endDate)
                            || (startDate >= startDateTime && endDateTime >= endDate))
                            &&
                            ((startTime <= starttime && starttime <= endTime)
                        || (startTime <= endtime && endtime <= endTime)
                        || (startTime >= starttime && endtime >= endTime)))
                        {
 
                            Label LabelUnavailable = (Label)gridViewRow.FindControl("Label1");
                            CheckBox CheckBoxAsset = (CheckBox)gridViewRow.FindControl("CheckBox1");
                            CheckBoxAsset.Visible = false;
                            LabelUnavailable.Visible = true;
 

                        }
 
                        else
                        {
 

 
                            Label LabelUnavailable = (Label)gridViewRow.FindControl("Label1");
                            CheckBox CheckBoxAsset = (CheckBox)gridViewRow.FindControl("CheckBox1");
                            CheckBoxAsset.Visible = true;
                            LabelUnavailable.Visible = false;
 

 
                        }
                    }
 
                }
                reader.Close();
 
                con.Close();
 

            
        }
 
    }
}

I would really appreciate any suggestion. 

Thanks

Answers (1)