gridview中列之间的总和

gridview中列之间的总和

本文介绍了gridview中列之间的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题我有3个coloumns来计算总和并显示在总列中但是下面的代码有错误,每当你输入null值它会给我

my problem i have 3 coloumns to make sum and display in total column but the code below have mistake with whenever u put null value it will give me

Input string was not in a correct format.

它只允许所有字段满值,所以plz plz

plz一些帮助我想允许空值和thx



我尝试过:



its only allow all field to be full with value so plz plz
plz some 1 help i want to allow null value and thx

What I have tried:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string val1 = e.Row.Cells[3].Text; //Gets the value in Column 1
            string val2 = e.Row.Cells[4].Text; //Gets the value in Column 2
            string val3 = e.Row.Cells[5].Text; //Gets the value in Column 3
            Label lblTotal = (Label)e.Row.Cells[6].FindControl("Label1"); //

            if (string.IsNullOrEmpty(val1))
            { val1 = "0"; }
            if (string.IsNullOrEmpty(val2))
            { val2 = "0"; }
            if (string.IsNullOrEmpty(val3) )
            { val3 = "0"; }


            Int64 sum = Int64.Parse(val1) + Int64.Parse (val2) + Int64.Parse (val3);
            lblTotal.Text += sum.ToString();

        }

推荐答案

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string val1 = e.Row.Cells[3].Text; //Gets the value in Column 1
            string val2 = e.Row.Cells[4].Text; //Gets the value in Column 2
            string val3 = e.Row.Cells[5].Text; //Gets the value in Column 3
            Label lblTotal = (Label)e.Row.Cells[6].FindControl("Label1"); //

             int _val1, _val2, _val3;
             int.TryParse(val1, out _val1);
             int.TryParse(val2, out _val2);
             int.TryParse(val3, out _val3);

            int sum = _val1 + _val2 + _val3;

            lblTotal.Text += sum.ToString();

        }
    }


Quote:

输入字符串格式不正确。

Input string was not in a correct format.



此消息告诉您尝试处理的数据中有错误或意外。我们无法为您检查,在收到错误消息时使用调试器并检查变量。



当您不明白您的代码正在做什么或为什么它做它做的,答案是调试器

使用调试器来查看你的代码在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



[]

[]

[]



调试器在这里向您展示您的代码在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


this message tells you that there is something wrong or unexpected in the data you try to handle. We can't check that for you, use the debugger and inspect variables when you get the error message.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于gridview中列之间的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:56