从gridview获取特定值而不使用dataformatstring

时间:2017-05-31 07:58:22

标签: c# asp.net gridview

当我的BoundField我需要放一个特定的datafromatstring dataformatstring="{0:###,###}"

后来我想从gridView获取这些值,当我得到它时,我有奇怪的格式,所以我HTMLDecode它然后我得到一个像450 000的值,但我不能从这个值中删除该空间。

if (e.Row.RowType == DataControlRowType.Footer)
    {
        int sum = 0;
        e.Row.Cells[0].Text = "Summary:";

        for (int j = 1; j < grid_summary.Columns.Count; ++j)
        {
        for (int i = 0; i < grid_summary.Rows.Count; ++i)
        {
            if (!grid_summary.Rows[i].Cells[j].Text.Equals("&nbsp;"))
            {
                string columnValueDecode = HttpUtility.HtmlDecode(grid_summary.Rows[i].Cells[j].Text);
                columnValueDecode.Replace(" ", "");
                columnValueDecode.Replace(" ", "").Replace("\t", "").Replace("\n", "").Replace("\r", "");
                columnValueDecode.Trim();
                sum += Convert.ToInt32(columnValueDecode);
            }

        }            
        // Display the summary data in the appropriate cells
        e.Row.Cells[j].Text = Convert.ToString(sum);

2 个答案:

答案 0 :(得分:0)

更改

if (!grid_summary.Rows[i].Cells[j].Text.Equals("&nbsp;"))
{
    string columnValueDecode = HttpUtility.HtmlDecode(grid_summary.Rows[i].Cells[j].Text);
    columnValueDecode.Replace(" ", "");
    columnValueDecode.Replace(" ", "").Replace("\t", "").Replace("\n", "").Replace("\r", "");
    columnValueDecode.Trim();
    sum += Convert.ToInt32(columnValueDecode);
}

if (!grid_summary.Rows[i].Cells[j].Text.Equals("&nbsp;"))
{
    string columnValueDecode = HttpUtility.HtmlDecode(grid_summary.Rows[i].Cells[j].Text);
    columnValueDecode = columnValueDecode.Trim().Replace(" ", "").Replace("\t", "").Replace("\n", "").Replace("\r", "");
    sum += Convert.ToInt32(columnValueDecode);
}

事实上,在网格数据源上执行这些操作更安全。

答案 1 :(得分:0)

我找到了答案:

string columnValueDecode = HttpUtility.HtmlDecode(grid_summary.Rows[i].Cells[j].Text);
    columnValueDecode = Regex.Replace(columnValueDecode, @"\s+", "");
    sum += Convert.ToInt32(columnValueDecode);