下面方法,只能在Gridview的BoundField进行操作,而在TemplateField模版中去找的话,就无能为力了,因TemplateField模版没有DataField属性。

代码

 public int GetColumnIndex(GridView gridView, string fieldName)
        {
            int index = -1;
            for (int i = 0; i < gridView.Columns.Count; i++)
            {
                DataControlField dataControlField = gridView.Columns[i];
                BoundField boundField = dataControlField as BoundField;               
                if (boundField != null && boundField.DataField == fieldName)
                    index =  i;
            }
            return index;
        }

或者可以利用HeaderText与FieldName进行比较,返回列索引:

代码

 public int GetColumnIndex(GridView gridview, string fieldName)
        {
            int index = -1;
            foreach (DataControlField dataControlField in gridview.Columns)
            {
                if (dataControlField.HeaderText == fieldName)
                {
                    index = gridview.Columns.IndexOf(dataControlField);
                }
            }
            return index;
        }

在Gridview控件中根据Field Name来取得对应列索引相关推荐

  1. [翻译]Scott Mitchell 的ASP.NET 2.0数据教程之十二:在GridView控件中使用TemplateField

    在ASP.NET 2.0中操作数据:在GridView控件中使用TemplateField 英文原版  |   本教程的代码(C#)   |   翻译目录   |   原文目录 导言 GridView ...

  2. Scott Mitchell 的ASP.NET 2.0数据教程之十二:在GridView控件中使用TemplateField

    导言 GridView是由一组字段(Field)组成的,它们都指定的了来自DataSource中的什么属性需要用到自己的输出呈现中.最简单的字段类型是BoundField,它仅将数据简单的显示为文本. ...

  3. 在ASP.NET 2.0中操作数据:在GridView控件中使用TemplateField

    英文原版  |   本教程的代码(C#)   |   翻译目录   |   原文目录 导言 GridView是由一组字段(Field)组成的,它们都指定的了来自DataSource中的什么属性需要用到 ...

  4. ASP.NET 2.0数据教程之十二:在GridView控件中使用TemplateField(转)

    导言 The GridView is composed of a set of fields that indicate what properties from the datasource are ...

  5. 在 GridView 控件中添加一列复选框51

    简介 在前面的教程中 , 我们学习了如何为 GridView 控件添加一列 单选 按钮来选择一个特定的记录.当用户被限制最多只能从网格中选中一项时,一列单选按钮是一个非常恰当的用户界面.然而,有时我们 ...

  6. 【ASP.NET】第八课——GridView 控件的编辑功能优化,GridView控件中嵌套DropDownList控件

    知识点:掌握 GridView 的编辑.高亮显示的功能 .GridView控件中嵌套DropDownList控件获取数据源. [ASP.NET]第七课--数据绑定和 GridView 控件的使用 重点 ...

  7. WebForm中GridView控件中添加一列按钮实现跳转传参

    在ToolBox中拖动GridView控件 点击箭头编辑Columns 双击添加HyperLinkField 具体设置properties 还可以使用代码编写 <asp:HyperLinkFie ...

  8. 获取 Windows 窗体 DataGridView 控件中选定的单元格、行和列

    获取 DataGridView 控件中选中的单元格 使用 SelectedCells 属性. 注意 使用 AreAllCellsSelected 方法可避免显示可能具有大量数据的单元格. Visual ...

  9. GridView控件中的一些常见问题

    1. 无法获取模板列中的值,使用FindControl()方法无效: 给模板列中添加隐藏域,并给隐藏域绑定要获取的值,代码如下: <asp:HiddenField ID="hfIsFr ...

最新文章

  1. 分享Kali Linux 2016.2第49周虚拟机
  2. python 使用UUID库生成唯一ID
  3. Linux free命令
  4. C语言再学习 -- 修改linux文件权限
  5. 互联网业界7月三件大事
  6. Hadoop配置机架感知
  7. 小程序和android联调,小程序打开APP指定页面
  8. PHP curl 直接获取请求变量,不直接输出
  9. Android 系统(194)---Android实践 -- 设置系统日期时间和时区
  10. python max_Python max()
  11. 大数据可视化方法有哪些
  12. RXJAVA之Subject
  13. 成为全栈工程师的6个要素
  14. 定制Android关机界面
  15. H5拍照、选择图片上传组件核心
  16. python数据处理系列之读写csv数据
  17. 活动|美团0.98充10元话费,秒到账,不实名不绑卡,超简单!
  18. 用ARIMA模型做需求预测
  19. 经典伴读_GOF设计模式_结构型模式
  20. spring源码学习之整合Mybatis原理分析

热门文章

  1. spring中是如何解析@Profile注解的
  2. spoj450 Enormous Input Test
  3. docker 网络 实现
  4. 练习题 James and Dominoes
  5. SpringBoot+Redis环境搭建
  6. 一 Storm 基础
  7. git log 查看提交记录,参数:
  8. mini6410移植--uboot移植(2)
  9. 用CSS控制表格的框格线
  10. 本人转行,出售二手.net书籍和源码,书籍3-4折,都是新华书店原版书。