如何遍历 GridView 中的每一行、每一列和单元格并获取其值

编程入门 行业动态 更新时间:2024-10-24 16:33:12
本文介绍了如何遍历 GridView 中的每一行、每一列和单元格并获取其值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个数据绑定的 GridView,在 button_click 我想逐行读取 GridView 每列并读取一行中每个单元格的值并更新数据库中的表?我也知道如何检查该单元格是否包含空值.

I have a GridView which is databound, on button_click I want to read the GridView row by row each column and read the value of each cell in a row and update a table in database? I also know how to check if that cell contains null.

我正在尝试这样的事情但卡住了:

I am trying something like this and got stuck:

protected void SAVE_GRID_Click(object sender, EventArgs e)
{
    int rowscount = GridView2.Rows.Count;
    int columnscount = GridView2.Columns.Count;
    for (int i = 0; i < rowscount; i++)
    {
        for (int j = 1; j < columnscount; j++)
        {
            // I want to get data of each cell in a row
            // I want to read the corresponding header and store
        }
    }       
}

推荐答案

最简单的方法是使用 foreach:

The easiest would be using a foreach:

foreach(GridViewRow row in GridView2.Rows)
{
    // here you'll get all rows with RowType=DataRow
    // others like Header are omitted in a foreach
}

根据您的编辑,您访问的列不正确,您应该从 0 开始:

According to your edits, you are accessing the column incorrectly, you should start with 0:

foreach(GridViewRow row in GridView2.Rows)
{
    for(int i = 0; i < GridView2.Columns.Count; i++)
    {
        String header = GridView2.Columns[i].HeaderText;
        String cellText = row.Cells[i].Text;
    }
}

这篇关于如何遍历 GridView 中的每一行、每一列和单元格并获取其值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-01 10:46:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/828279.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:遍历   并获   单元格   取其   GridView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!