This is a migrated thread and some comments may be shown as answers.

how to average a row in gridview

1 Answer 80 Views
GridView
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 12 Jul 2012, 03:53 PM
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();

                        }

                    }
                }
            }
        }

1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 16 Jul 2012, 12:20 PM
Hello John,

Thank you for writing.

If I understand correctly, you want to display the average value of your TotalTime column in a text box. If so, you will have to manually calculate the average in the different scenarios and populate the text box. Such scenarios can be - adding row, changing cell value, deleting row, etc.

More convenient will be to use the default functionality of RadGridView for SummaryRows. You can read more about it here: http://www.telerik.com/help/winforms/gridview-rows-summary-rows.html.

I hope that you find my answer helpful.

All the best,
Stefan
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
Tags
GridView
Asked by
John
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Share this question
or