如何在gridview控件中循环遍历单元格

如何在gridview控件中循环遍历单元格

本文介绍了如何在gridview控件中循环遍历单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,请问我有一个gridview表的循环问题。我有一个像这样的gridview表

Good day, please i'm having loop issues with a gridview table. i have a gridview table like this

+--------------+---------+---------+---------+---------+---------+---------+
| Values       | Col1    | Col2    | Col3    | Col4    | Col5    | Col6    |
+--------------+---------+---------+---------+---------+---------+---------+
| Row1         | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    |
+--------------+---------+---------+---------+---------+---------+---------+
| Row2         | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    |
+--------------+---------+---------+---------+---------+---------+---------+
| Row3         | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    |
+--------------+---------+---------+---------+---------+---------+---------+
| Row4         | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    |
+--------------+---------+---------+---------+---------+---------+---------+



我想要循环所有带有数字的单元格并添加数字给我一个看起来像这样的表格




I want to loop through all the cells with numbers and add the numbers to give me a table that looks like this

+--------------+---------+---------+---------+---------+---------+---------+
| Values       | Col1    | Col2    | Col3    | Col4    | Col5    | Col6    |
+--------------+---------+---------+---------+---------+---------+---------+
| Row1         | 3       | 3       | 3       | 3       | 3       | 3       |
+--------------+---------+---------+---------+---------+---------+---------+
| Row2         | 3       | 3       | 3       | 3       | 3       | 3       |
+--------------+---------+---------+---------+---------+---------+---------+
| Row3         | 3       | 3       | 3       | 3       | 3       | 3       |
+--------------+---------+---------+---------+---------+---------+---------+
| Row4         | 3       | 3       | 3       | 3       | 3       | 3       |
+--------------+---------+---------+---------+---------+---------+---------+





这就是我的尝试





This is what i have tried

protected void add(object sender, EventArgs e)
        {
            char[] chars = {','};
            try
            {
                for (int j = 0; j < excel.Rows.Count; j++)
                {

                    for (int k = 1; k < excel.Rows.Count; k++)
                    {
                        string a = excel.Rows[j].Cells[k].Text; // this issues an index out of range error

                        string [] scores = a.Split(chars);

                        string ba = scores.ElementAt(0);
                        string ca = scores.ElementAt(1);

                        int b = Convert.ToInt32(ba);
                        int c = Convert.ToInt32(ca);

                        int total = b + c;
                        string tot = total.ToString();

                        excel.Rows[j].Cells[k].Text = tot; // this assigns the cell the value of the sum
                    }
                }
            }
            catch (MySqlException myex)
            {
                error.Text = myex.Message;
            }
            catch (Exception ex)
            {
                error.Text = ex.Message;
            }

        }





但是这给了我这样的结果并发布索引这行代码中的范围异常



but this gives me a result like this and issurs an index out of range exception on this line of code

string a = excel.Rows[j].Cells[k].Text;







+--------------+---------+---------+---------+---------+---------+---------+
| Values       | Col1    | Col2    | Col3    | Col4    | Col5    | Col6    |
+--------------+---------+---------+---------+---------+---------+---------+
| Row1         | 3       | 3       | 3       | 3       | 3       | 3       |
+--------------+---------+---------+---------+---------+---------+---------+
| Row2         | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    |
+--------------+---------+---------+---------+---------+---------+---------+
| Row3         | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    |
+--------------+---------+---------+---------+---------+---------+---------+
| Row4         | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    | 1, 2    |
+--------------+---------+---------+---------+---------+---------+---------+





任何帮助都很可爱。谢谢



any assistance would be lovely. Thanks

推荐答案

for (int j = 0; j < excel.Rows.Count; j++)
{

    for (int k = 1; k < excel.Rows.Count; k++)



但是 k 计数器必须以<$结尾c $ c> excel.Rows [j] .Cells.Count 或 excel.Columns.Count



请参阅:

[]

]


but k counter have to ends with excel.Rows[j].Cells.Count or excel.Columns.Count

See:
GridView.Columns Property[^]
How to: Set GridView Web Server Control Column Width Dynamically[^]


foreach (GridViewRow j in gridview1.Rows)
{
    for (int k = 1; k < j.Cells.Count; k++)
    {
        string a = j.Cells[k].Text;
        string [] scores = a.Split(chars);
        string ba = scores.ElementAt(0);
        string ca = scores.ElementAt(1);
        int b = Convert.ToInt32(ba);
        int c = Convert.ToInt32(ca);
        int total = b + c;
        string tot = total.ToString();
        j.Cells[k].Text = tot; // this assigns the cell the value of the sum
    }

}                    


这篇关于如何在gridview控件中循环遍历单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 16:13