【问题标题】:Accessing Gridview columns by Row.Cells通过 Row.Cells 访问 Gridview 列
【发布时间】:2011-05-11 05:01:46
【问题描述】:
嘿,我正在使用以下代码访问一行中的列,但在使用 Row.Cells[1].Text 时总是显示空字符串我在 GridView Unload 事件处理程序中使用此代码。
提前感谢客栈。
foreach (GridViewRow row in grvSearchRingTone.Rows)
{
String coltext = row.Cells[1].Text;
}
【问题讨论】:
标签:
c#
.net
asp.net
gridview
【解决方案1】:
我建议改用Gridview Databound event。因为
从内存中卸载服务器控件时发生卸载事件。
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow row in grvSearchRingTone.Rows)
{
String coltext = row.Cells[1].Text;
}
}
数据绑定事件在服务器控件绑定到数据源后发生。
要了解 gridview 事件的工作原理,请查看MSDN
【解决方案2】:
可以在 Gridviews RowDataBound Event ie 中完成
protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
String coltext = e.Row.Cells[1].Text
}
else if(e.Row.RowType == DataControlRowType.DataRow)
{
String coltext = e.Row.Cells[1].Text
}
}
希望这会有所帮助。