DataTable 获取一列最大值并修改

DataTable中获取分数的最大值

1、创建DataTable并赋值

DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Score", typeof(double));
            //设置datable主键列
            dt.PrimaryKey = new DataColumn[] { dt.Columns["ID"] };

            #region 添加数据
            DataRow dr = null;
            for (int i = 0; i < 10; i++)
            {
                dr = dt.NewRow();
                dr["ID"] = 100 + i.ToString();
                dr["Name"] = "Name" + i.ToString();
                dr["Score"] = (i + 1) * 30;

                dt.Rows.Add(dr);
            }
            #endregion
View Code

2、找出DataTable中分数的最大值

            double maxScore = 0;
            double.TryParse(dt.Compute("Max(Score)", "").ToString(), out maxScore);// 取得datable中Score的最大值
            DataRow row = dt.Select("Score=" + maxScore)[0];    
View Code

3、修改最大分数值

row["Score"] = 100000000.12;
View Code

 

posted on 2019-11-11 16:11  lovezj9012  阅读(632)  评论(0编辑  收藏  举报

导航