现在在做一个工资的查询系统,有两个问题,请教大神
第一,能不能把图中的0.00替换成---,并且还能保持计算。
计算代码
public decimal ReturnTotal(int col)
{
decimal char_total = 0;
foreach (GridViewRow gvr in GridView1.Rows)
{
if (null != gvr.Cells[col].Text)
{
char_total += Convert.ToDecimal(gvr.Cells[col].Text);
}
}
return char_total;
}
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[2].Text = "小计";
e.Row.Cells[3].Text = ReturnTotal(3).ToString();
e.Row.Cells[4].Text = ReturnTotal(4).ToString();
e.Row.Cells[5].Text = ReturnTotal(5).ToString();
e.Row.Cells[7].Text = "合计:"+ Convert.ToString(ReturnTotal(3) + ReturnTotal(4) + ReturnTotal(5));
}
}
public decimal ReturnTotal(int col)
{
decimal char_total = 0;
foreach (GridViewRow gvr in GridView1.Rows)
{
if (null != gvr.Cells[col].Text && gvr.Cells[col].Text != "---")
{
char_total += Convert.ToDecimal(gvr.Cells[col].Text);
}
}
return char_total;
}
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
e.Row.Cells[3].Text = e.Row.Cells[3].Text == "0.00" ? "---" : e.Row.Cells[3].Text;
e.Row.Cells[4].Text = e.Row.Cells[4].Text == "0.00" ? "---" : e.Row.Cells[4].Text;
e.Row.Cells[5].Text = e.Row.Cells[5].Text == "0.00" ? "---" : e.Row.Cells[5].Text;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[2].Text = "小计";
e.Row.Cells[3].Text = ReturnTotal(3).ToString();
e.Row.Cells[4].Text = ReturnTotal(4).ToString();
e.Row.Cells[5].Text = ReturnTotal(5).ToString();
e.Row.Cells[7].Text = "合计:"+ Convert.ToString(ReturnTotal(3) + ReturnTotal(4) + ReturnTotal(5));
}
}
private static string connectionString = string.Empty;
private static ExcelExtention excelExtention = ExcelExtention.xls;
/// <summary>
/// 将数据源读成数据源
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static DataSet Read(string filename)
{
excelExtention = GetExcelExtention(filename);
return Read(filename, GetWorkSheets(GetConnectionString(filename)));
}
private static ExcelExtention GetExcelExtention(string filename)
{
string extention = filename.Split('.')[filename.Split('.').Length - 1];
if (extention.Trim() == ExcelExtention.xlsx.ToString())
return ExcelExtention.xlsx;
else
return ExcelExtention.xls;
}
/// <summary>
/// 返回连接excel的字符串
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
private static string GetConnectionString(string filename)
{
if (excelExtention == ExcelExtention.xls)
connectionString = string.Format("Provider=Microsoft.Jet.OleDb.4.0;data source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'", filename);
else if (excelExtention == ExcelExtention.xlsx)
connectionString = string.Format("Provider=Microsoft.ACE.OleDb.12.0; data source={0}; Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'", filename);
return connectionString;
}
public static DataSet Read(string filename, string[] workSheets)
{
var ds = new DataSet();
foreach (string workSheet in workSheets)
{
try
{
using (OleDbDataAdapter adapter = new OleDbDataAdapter(string.Format("SELECT * FROM [{0}]", workSheet), GetConnectionString(filename)))
{
adapter.Fill(ds, workSheet);
}
}
catch (Exception ex)
{
//LogHelper.Log(string.Format("将Excel工作表【{0}】数据填充到数据集中失败,原因如下:{1}", workSheet, ex.ToString()));
throw new Exception(ex.Message, ex.InnerException);
}
}
return ds;
}
/// <summary>
///
/// </summary>
/// <param name="connectionString"></param>
/// <returns></returns>
private static string[] GetWorkSheets(string connectionString)
{
DataTable dataTable;
try
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
dataTable = connection.GetSchema("Tables");
}
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex.InnerException);
}
int lenght = dataTable.Rows.Count;
string[] worksheets = new string[lenght];
for (int i = 0; i < lenght; i++)
{
worksheets[i] = dataTable.Rows[i]["TABLE_NAME"].ToString();
}
return worksheets;
}
1// 类范围,累积合计的变量……
2decimal _totalUnitPrice = 0m;
3int _totalNonNullUnitPriceCount = 0;
4int _totalUnitsInStock = 0;
5int _totalUnitsOnOrder = 0;
6
7protected void ProductsInCategory_RowDataBound(object sender, GridViewRowEventArgs e)
8{
9 if (e.Row.RowType == DataControlRowType.DataRow)
10 {
11 // 通过e.Row.DataItem 属性引用ProductsRow
12 Northwind.ProductsRow product = (Northwind.ProductsRow)((System.Data.DataRowView)e.Row.DataItem).Row;
13
14 // 增加累积合计(如果它们不为NULL的话!)
15 if (!product.IsUnitPriceNull())
16 {
17 _totalUnitPrice += product.UnitPrice;
18 _totalNonNullUnitPriceCount++;
19 }
20
21 if (!product.IsUnitsInStockNull())
22 _totalUnitsInStock += product.UnitsInStock;
23
24 if (!product.IsUnitsOnOrderNull())
25 _totalUnitsOnOrder += product.UnitsOnOrder;
26 }
27}