PLC论坛-全力打造可编程控制器专业技术论坛

 找回密码
 注册哦

QQ登录

只需一步,快速开始

微信扫码登录

查看: 106|回复: 0

[三菱] c#实现自动化报表之一,查询生成基本的平均值、最大值、最小值

[复制链接]
发表于 2024-4-8 08:15:08 | 显示全部楼层 |阅读模式
上一篇文章简单的实现了C#连接MYSQL后,实现数据查询显示:
测试c#查询MYSQL数据库

本次我们做简单的查询排序及计算平均值、最小值、最大值。
一、重新排序

    上篇文章我们查询的序号直接是引用了mysql的序号,我们本次重新排序:
代码如下:
//this.dataGridView1.Rows[index].Cells[0].Value= reader.GetInt32("xh").ToString();//调用数据库本身的序号
this.dataGridView1.Rows[index].Cells[0].Value = num.ToString();//查询数据重新排序


二、计算平均值

2.1 计算所有查询的累计值

2.2 算术平均值

  this.dataGridView1.Rows.Add();//增加1行,用于显示平均值,
   this.dataGridView1.Rows[num].Cells[1].Value = "平均值";
   this.dataGridView1.Rows[num].Cells[2].Value = (a1 / num).ToString("#0.000");//平均值保留三位小数
   this.dataGridView1.Rows[num].Cells[3].Value = (a2 / num).ToString("f3");//平均值保留三位小数
   this.dataGridView1.Rows[num].Cells[4].Value = (a3 / num).ToString("f3");//平均值

三、计算最大值
var MaxID1 = dataGridView1.Rows.Cast<DataGridViewRow>()
         .Max(r => Convert.ToSingle(r.Cells[2].Value));  //取t1的最大值
var MaxID2= dataGridView1.Rows.Cast<DataGridViewRow>()
       .Max(r => Convert.ToSingle(r.Cells[3].Value));  //取t2的最大值
var MaxID3 = dataGridView1.Rows.Cast<DataGridViewRow>()
       .Max(r => Convert.ToSingle(r.Cells[4].Value));  //取t3的最大值
this.dataGridView1.Rows[num + 1].Cells[1].Value = "最大值";
this.dataGridView1.Rows[num+1].Cells[2].Value= MaxID1.ToString("#0.000");//t1最大值
this.dataGridView1.Rows[num+1].Cells[3].Value= MaxID2.ToString("#0.000");//t2最大值
this.dataGridView1.Rows[num+1].Cells[4].Value= MaxID3.ToString("#0.000");//t3最大值

四、计算最小值
var minID1 = dataGridView1.Rows.Cast<DataGridViewRow>()
               .Min(r => Convert.ToSingle(r.Cells[2].Value));  //取t1的最小值
var minID2 = dataGridView1.Rows.Cast<DataGridViewRow>()
             .Min(r => Convert.ToSingle(r.Cells[3].Value));  //取t2的最小值
var minID3 = dataGridView1.Rows.Cast<DataGridViewRow>()
             .Min(r => Convert.ToSingle(r.Cells[4].Value));  //取t3的最小值
this.dataGridView1.Rows.Add(2);//增加2行,用于显示最小值,最大值
this.dataGridView1.Rows[num + 2].Cells[1].Value = "最小值";
this.dataGridView1.Rows[num+2].Cells[2].Value= minID1.ToString("#0.000");//t1最小值
this.dataGridView1.Rows[num+2].Cells[3].Value= minID2.ToString("#0.000");//t2最小值
this.dataGridView1.Rows[num+2].Cells[4].Value= minID3.ToString("#0.000");//t3最小值

五、测试效果与修正


这里我们可以看出,平均值和最小值之间有一行空数据,为此我们采用以下语句删除空行。

  dataGridView1.Rows.RemoveAt(num + 1);//删除空行数据
修正后测试效果:









免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册哦

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册哦

本版积分规则

QQ|小黑屋|手机版|Archiver|PLC技术网-PLC论坛 ( 粤ICP备17165530号 )|网站地图

GMT+8, 2024-5-4 23:58 , Processed in 0.048352 second(s), 26 queries .

快速回复 返回顶部 返回列表