for (int i = 9; i < dt.Columns.Count; i++)
{
string dtcolumn = dt.Columns[i].ColumnName.ToString();
dt.Rows[dt.Rows.Count - 1][i] = Convert.ToInt32(dt.Compute("SUM(" + dtcolumn + ")", " " + dtcolumn + " > 0"));
}
它在列值不为空时正在工作。但有时抛出invalidcastexception和显示对象不能从DBNULL转换到其他types.how以避免invalidcastexception获取列名并对数据表中的值求和c#
没有什么不对您的代码,至少无法与已发布的部分。您需要告诉我们如何重现您的问题。 – CodingYoshi
你可以尝试'dt.Compute(“SUM(IsNull([”+ dtcolumn +“],0))”'https://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression – Slai