如何得到gridview的“列索引”

wang520d 2008-05-14 11:13:50
注意是列索引 为什么要获得列索引:因为我要在下面事件中根据列索引去隐藏列 可是我只知道 绑定列的字段名 不知道这个字段的cells索引值


protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow ||
e.Row.RowType == DataControlRowType.Header)
{
//e.Row.Cells[这个值怎么根据字段名得到].Visible = false; //
//e.Row.Cells[
//e.Row.Cells
//TableRow
//TableCellCollection
}
}
...全文
532 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
qdzhaolin 2009-09-10
  • 打赏
  • 举报
回复
public static int GetColumnIndexByDataField(GridView gv, string dataField)
{
int index = -1;
if (HttpContext.Current.Items[dataField.ToUpper() + "_INDEX"] == null)
{
foreach (DataControlField col in gv.Columns)
{
index++;
if (col.GetType() == typeof(HyperLinkField))
{
HyperLinkField hCol = (HyperLinkField)col;
if (hCol.DataTextField.ToUpper() == dataField.ToUpper())
{
HttpContext.Current.Items[dataField.ToUpper() + "_INDEX"] = index;
return index;
}
}
else if (col.GetType() == typeof(BoundField))
{
BoundField bCol = (BoundField)col;
if (bCol.DataField.ToUpper() == dataField.ToUpper())
{
HttpContext.Current.Items[dataField.ToUpper() + "_INDEX"] = index;
return index;
}
}
else if (col.GetType() == typeof(ButtonField))
{
ButtonField bCol = (ButtonField)col;
if (bCol.CommandName.ToUpper() == dataField.ToUpper())
{
HttpContext.Current.Items[dataField.ToUpper() + "_INDEX"] = index;
return index;
}
}
else if (col.GetType() == typeof(TemplateField))
{
TemplateField bCol = (TemplateField)col;
if (bCol.AccessibleHeaderText.ToUpper() == dataField.ToUpper())
{
HttpContext.Current.Items[dataField.ToUpper() + "_INDEX"] = index;
return index;
}
}
else if (col.GetType() == typeof(CommandField))
{
CommandField bCol = (CommandField)col;
if (bCol.AccessibleHeaderText.ToUpper() == dataField.ToUpper())
{
HttpContext.Current.Items[dataField.ToUpper() + "_INDEX"] = index;
return index;
}
}
}
index = -1;
}
else
{
index = (int)HttpContext.Current.Items[dataField.ToUpper() + "_INDEX"];
}
return index;
}
我只相信汗水 2009-01-20
  • 打赏
  • 举报
回复
不好意思,本人嘴笨,“你所知道的列名” 这里应是“你所知道的字段名”,是指字段
我只相信汗水 2009-01-20
  • 打赏
  • 举报
回复
你可以将GridView1的数据原逆转换成DataTable,通过DataTable来判断列索引,"这个列索引便是GRIDVIEW的列索引了"。

补充一点,刚才写的时候忘考虑绑定模板列的情况了,如果你的SQL是select field1,field2 from table,而GRIDVIEW绑定有模板列,比如表头是
选择(checkbox) 字段1 字段2,那么就应该是
for (int i = 0; i < dtb.Columns.Count; i++)
{
if (dtb.Columns[i].ToString() == "你所知道的列名")
{
ColIndex=i+1;//在数据列前面有1个模板列
break;
}
}
我只相信汗水 2009-01-20
  • 打赏
  • 举报
回复
[Quote=引用楼主 wang520d 的帖子:]
注意是列索引 为什么要获得列索引:因为我要在下面事件中根据列索引去隐藏列 可是我只知道 绑定列的字段名 不知道这个字段的cells索引值


C# code
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow ||
e.Row.RowType == DataControlRowType.Header)
{
//e.Row.Cells[这个值怎么根据字段名得…
[/Quote]

你可以将GridView1的数据原逆转换成DataTable,通过DataTable来判断列索引,这个列索引便是GRIDVIEW的列索引了。下面是我写的代码,没时间调试,不过应该是可以的。
int ColIndex=0;
DataTable dtb = (DataTable)GridView1.DataSource;
for (int i = 0; i < dtb.Columns.Count; i++)
{
if (dtb.Columns[i].ToString() == "你所知道的列名")
{
ColIndex=i;
break;
}
}
ColIndex就是你所得的列的索引。
46539492 2008-05-15
  • 打赏
  • 举报
回复
楼主可能是需要这些隐藏列吧?可以这样
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[13].Attributes.Add("style", "display:none;");
e.Row.Cells[14].Attributes.Add("style", "display:none;");
e.Row.Cells[15].Attributes.Add("style", "display:none;");
e.Row.Cells[16].Attributes.Add("style", "display:none;");
e.Row.Cells[17].Attributes.Add("style", "display:none;");

}
wwb8 2008-05-15
  • 打赏
  • 举报
回复
6 7 10楼都已经告诉你了
virusswb 2008-05-15
  • 打赏
  • 举报
回复
你可以这么做,比如说你要隐藏性别列,就循环e.rows.cells.count,如果列标题是性别就隐藏这个列就可以了
virusswb 2008-05-15
  • 打赏
  • 举报
回复
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[5].Visible = false;
}

http://shanyou.cnblogs.com/archive/2005/12/22/302936.html
kong521 2008-05-15
  • 打赏
  • 举报
回复
感觉楼主多此一举,在创建时隐藏不就好了,这样不浪费资源吗?
sxu_nono 2008-05-15
  • 打赏
  • 举报
回复
你可以在GridView里事先创建好列,指定对应的字段名,RowCreated的时候再隐藏……
不过既然如此,你要这个列干吗用?
阿非 2008-05-15
  • 打赏
  • 举报
回复

if (e.Row.Cells[i].Text = "特定列名")


应该为:

if (e.Row.Cells[i].Text == "特定列名")

阿非 2008-05-15
  • 打赏
  • 举报
回复

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
int index = -1;
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
// 不是字段名,是字段对应的列的列名
if (e.Row.Cells[i].Text = "特定列名")
{
e.Row.Cells[i].Visible = false;
index = i;
}
}
}
else if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (i == index)
{
e.Row.Cells[i].Visible = false;
}
}
}
}
yl_801 2008-05-15
  • 打赏
  • 举报
回复
0、1、2……
这样数过去难道不行吗
Neil_Free 2008-05-15
  • 打赏
  • 举报
回复
rowindex
wang520d 2008-05-14
  • 打赏
  • 举报
回复
不会没人会吧。。
wang520d 2008-05-14
  • 打赏
  • 举报
回复
自己先顶出水面
wang520d 2008-05-14
  • 打赏
  • 举报
回复
居然没有人知道。。。。真日了。。。。。

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧