如何通过每个循环和每一行,列和单元在GridView和得到其值 [英] How to loop through each and every row, column and cells in a GridView and get its value
本文介绍了如何通过每个循环和每一行,列和单元在GridView和得到其值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 GridView控件
这是数据绑定,在button_click我想读通过行每列 GridView控件
行和读取每个单元的值在一排,并在数据库中更新表?我也知道如何检查该单元格中包含空值。
我想这样的事情,并卡住了:
保护无效SAVE_GRID_Click(对象发件人,EventArgs的发送)
{
INT rowscount = GridView2.Rows.Count;
INT columnscount = GridView2.Columns.Count;
的for(int i = 0; I< rowscount;我++)
{
对于(INT J = 1; J< columnscount; J ++)
{ //我想在一行中每个单元格的数据
//我想读相应的头和存储
}
} }
解决方案
最简单的将使用的foreach
:
的foreach(在GridView2.Rows GridViewRow行)
{
//在这里你会得到所有行与行类型=的DataRow
//其他象头在foreach省略
}
编辑:根据您的编辑,你是不正确的访问之列,你应该从0开始:
的foreach(在GridView2.Rows GridViewRow行)
{
的for(int i = 0; I< GridView2.Columns.Count;我++)
{
串标头= GridView2.Columns [I] .HeaderText;
字符串CELLTEXT = row.Cells [I]。文本;
}
}
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
}
}
}
解决方案
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
}
Edit: 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屋!
查看全文