本文介绍了如何遍历 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:关键词]
发布评论