I am trying to add rows to a datagrid where a condition is passed to query the data and get the results returned from that query to add to a datagrid. Below is the code:
private void FetchAllJobStatus(int regionID)
{
OleDbConnection con = new OleDbConnection(Constring);
String strQu1;
strQu1 = "SELECT LOGPATH, TWSID FROM JOB_DETAILS_TEST WHERE REGIONID = " + regionID + " ORDER BY TWSID";
OleDbDataAdapter dapt1 = new OleDbDataAdapter(strQu1, con);
DataTable dt1 = new DataTable();
dapt1.Fill(dt1);
foreach (DataRow row in dt1.Rows)
{
string twsIDName = row.ItemArray[1].ToString();
string startTime = GetStartTime(row.ItemArray[0].ToString(), int.Parse(twsIDName));
string endTime = GetEndTime(row.ItemArray[0].ToString(), int.Parse(twsIDName));
DateTime dat1 = DateTime.Parse(startTime);
DateTime dat2 = DateTime.Parse(endTime);
string endingTime;
if (endTime == string.Empty || endTime == null || dat2 < dat1)
{
endingTime = "";
}
else
{
endingTime = endTime.Remove(0, 3).ToString();
}
string startingTime = startTime.Remove(0, 3).ToString();
String strQu2;
strQu2 = "SELECT JOBNAME, TWSID, HIGHPRIORITY, DAY, TIME, '"+ startingTime + "' as StartTime, '"+ endingTime + "' as EndTime FROM JOB_DETAILS_TEST WHERE TWSID = " + int.Parse(twsIDName) + " AND REGIONID = " + regionID + " ORDER BY JOBNAME";
OleDbDataAdapter dapt2 = new OleDbDataAdapter(strQu2, con);
DataSet ds = new DataSet();
dapt2.Fill(ds, "dt2");
dgJobStatusAll.AutoGenerateColumns = true;
dgJobStatusAll.DataSource = ds;
dgJobStatusAll.DataMember = "dt2";
}
}
Here only the last data gets appended in the grid. Thanks in advance
write a single query using union and fill the datatable out side the loop. Take
out of the loop.And in loop use
And now After the loop write
And fill datatable and bind to grid