im populating a data gridview from a sql table and once the information is in the grid view i want to take the average of the 5th row called TotalTime i need the average time it takes to build the product and put it in to a text box and put it in to a text box labeled Tb_AverageTime
my code so far
string _connStr = "Server = ESIDC2\\ALLORDERS,62800; User ID=sa; password=Sysadmin1;Database=ESIVENTURES";
using (SqlConnection conn = new SqlConnection(_connStr))
{
conn.Open();
string sql = "SELECT AssemblyWhen, AssemblyWho, AssemblyDevID, AssemblyTimeLength FROM ESI_AssemblyTime WHERE AssemblyDevID = 'AOS-1';";
SqlDataReader rdr = new SqlCommand(sql, conn).ExecuteReader();
if (!rdr.HasRows)
{
rdr.Close();
// ADD WHAT TO DO WHEN THERE IS NO DATA
MessageBox.Show("No Data");
}
if (rdr.HasRows)
{
rdr.Read();
while (rdr.Read())
{
if ((rdr["AssemblyWhen"] != DBNull.Value) && (rdr["AssemblyWho"] != DBNull.Value) && (rdr["AssemblyDevID"] != DBNull.Value) && (rdr["AssemblyTimeLength"] != DBNull.Value))
{
GridViewInfo info = new GridViewInfo(Aos1_GridView.MasterTemplate);
GridViewDataRowInfo rowInfo = new GridViewDataRowInfo(info);
rowInfo.Cells["PartName"].Value = rdr["AssemblyDevID"].ToString();
rowInfo.Cells["DateMade"].Value = rdr["AssemblyWhen"].ToString();
rowInfo.Cells["Employee"].Value = rdr["AssemblyWho"].ToString();
rowInfo.Cells["TotalTime"].Value = rdr["AssemblyTimeLength"].ToString();
Aos1_GridView.Rows.Add(rowInfo);
TB_PartsDone.Text = Aos1_GridView.Rows.Count.ToString();
}
}
}
}
}
my code so far
string _connStr = "Server = ESIDC2\\ALLORDERS,62800; User ID=sa; password=Sysadmin1;Database=ESIVENTURES";
using (SqlConnection conn = new SqlConnection(_connStr))
{
conn.Open();
string sql = "SELECT AssemblyWhen, AssemblyWho, AssemblyDevID, AssemblyTimeLength FROM ESI_AssemblyTime WHERE AssemblyDevID = 'AOS-1';";
SqlDataReader rdr = new SqlCommand(sql, conn).ExecuteReader();
if (!rdr.HasRows)
{
rdr.Close();
// ADD WHAT TO DO WHEN THERE IS NO DATA
MessageBox.Show("No Data");
}
if (rdr.HasRows)
{
rdr.Read();
while (rdr.Read())
{
if ((rdr["AssemblyWhen"] != DBNull.Value) && (rdr["AssemblyWho"] != DBNull.Value) && (rdr["AssemblyDevID"] != DBNull.Value) && (rdr["AssemblyTimeLength"] != DBNull.Value))
{
GridViewInfo info = new GridViewInfo(Aos1_GridView.MasterTemplate);
GridViewDataRowInfo rowInfo = new GridViewDataRowInfo(info);
rowInfo.Cells["PartName"].Value = rdr["AssemblyDevID"].ToString();
rowInfo.Cells["DateMade"].Value = rdr["AssemblyWhen"].ToString();
rowInfo.Cells["Employee"].Value = rdr["AssemblyWho"].ToString();
rowInfo.Cells["TotalTime"].Value = rdr["AssemblyTimeLength"].ToString();
Aos1_GridView.Rows.Add(rowInfo);
TB_PartsDone.Text = Aos1_GridView.Rows.Count.ToString();
}
}
}
}
}