我正在尝试对“薪水”一栏求和。下面是我的桌子。
public static void dataTable()
{
DataTable table = GetTable();
}
static DataTable table;
public static DataTable GetTable()
{
table = new DataTable();
table.Columns.Add("player", typeof(string));
table.Columns.Add("age", typeof(double));
table.Columns.Add("position", typeof(stats.pos));
table.Columns.Add("ovalue", typeof(double));
table.Columns.Add("dvalue", typeof(double));
table.Columns.Add("team", typeof(stats.team));
table.Columns.Add("salary", typeof(int));
table.Columns.Add("contractYears", typeof(int));
table.Columns.Add("active", typeof(bool));
table.Rows.Add("AG", 24, stats.pos.SP, 0, 4.2, stats.team.S, 1, 5, true);
table.Rows.Add("AR", 30, stats.pos.SP, 0, 2.6, stats.team.S, 7, 2, true);
table.Rows.Add("JK", 22, stats.pos.SP, 0, 3.2, stats.team.S, 1, 6, true);
}
public class stats
{
public enum pos { fiB, seB, SS, thB, OF, C, DH, SP, RP };
public enum team { S };
}
这是我添加薪水列的尝试:
try
{
object sumobject = (object)table.Compute("Sum(salary)", "active=true");
double sumdouble = Convert.ToDouble(sumobject);
payrollLabel.Text = Convert.ToString(sumdouble);
}
catch
{
MessageBox.Show("Error in payroll");
}
但是,代码总是跳到捕获处。它说我的列中有一个空值,但我没有输入任何值。任何帮助将不胜感激。