查了下资料,可以用聚合列 实现列的求和、乘除等计算 strSQL = "select b.TeacherName,sum(case b.Satisfaction when 'A' then 1 else 0 end) as best," + "sum(case b.Satisfaction when 'B' then 1 else 0 end) as better," + "sum(case b.Satisfaction when 'C' then 1 else 0 end) as bad " + "from dc_voteT as b where b.SYSID=" + myData.SYS_ID + " group BY b.TeacherName"; //MessageBox.Show(strSQL); cmd = new SqlCommand(strSQL, conn); sda.SelectCommand = cmd; sda.Fill(ds, "FF");//计算列 DataColumn zb1 = new DataColumn(); zb1.DataType = System.Type.GetType("System.Double"); zb1.Caption = "add"; zb1.ColumnName = "best占比"; zb1.Expression = "best/(best+better+bad)"; //注意是用ColumnName属性引用上面两个列 ds.Tables["FF"].Columns.Add(zb1);
dv.Sort = "序号";
datatable也有select方法,可以筛选记录
{
DataTable dataTable=new DataTable();
dataTable.Columns.Add("ID");
dataTable.Columns.Add("Sort",Type.GetType("System.Int32"));//设定类型
Random random=new Random();
for (int j = 0; j < 20; j++)
{
DataRow dataRow = dataTable.NewRow();
dataRow["ID"] = j;
dataRow["Sort"] = random.Next(1, 100);
dataTable.Rows.Add(dataRow);
}
DataRow[] dataRows= dataTable.Select(null, "Sort desc");//排序条件
foreach (DataRow row in dataRows)
{
Console.WriteLine(row["Sort"].ToString());
}
Console.Read();
}
+ "sum(case b.Satisfaction when 'B' then 1 else 0 end) as better,"
+ "sum(case b.Satisfaction when 'C' then 1 else 0 end) as bad "
+ "from dc_voteT as b where b.SYSID=" + myData.SYS_ID + " group BY b.TeacherName"; //MessageBox.Show(strSQL);
cmd = new SqlCommand(strSQL, conn);
sda.SelectCommand = cmd;
sda.Fill(ds, "FF");//计算列
DataColumn zb1 = new DataColumn();
zb1.DataType = System.Type.GetType("System.Double");
zb1.Caption = "add";
zb1.ColumnName = "best占比";
zb1.Expression = "best/(best+better+bad)"; //注意是用ColumnName属性引用上面两个列
ds.Tables["FF"].Columns.Add(zb1);
那么现在的问题是,怎么指定按照新添加的列zb1进行排序?
ds.Tables["FF"].DefaultView.Sort = "best占比";新的问题是,怎么降序排列?
准备结贴了。