惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

人人都是产品经理
人人都是产品经理
宝玉的分享
宝玉的分享
小众软件
小众软件
有赞技术团队
有赞技术团队
月光博客
月光博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
MyScale Blog
MyScale Blog
Engineering at Meta
Engineering at Meta
Stack Overflow Blog
Stack Overflow Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
D
Docker
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
MongoDB | Blog
MongoDB | Blog
WordPress大学
WordPress大学
J
Java Code Geeks
罗磊的独立博客
V
Visual Studio Blog
雷峰网
雷峰网
H
Help Net Security
T
The Blog of Author Tim Ferriss
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
F
Fortinet All Blogs

博客园 - 赣江源

StreamInsight参考示例 Microsoft StreamInsight简介 GridView中的DataFormatString失效解决办法 - 赣江源 - 博客园 按照时间点还原数据库(SQL Server) 如何显示在线用户? ASP.NET中每个页面在Load之前调用公用函数 网站单点登录的实现思路 在服务器端提取ASP.NET控件输出的HTML 如何处理在IE7关闭时不出现选项卡提示 使用JavaScript控制UpdatePanel的更新 如何在GridView将数字显示成金额格式或自定义格式? ASP.NET 2.0中使用XSLT的变化 在GridView表头显示排序方向 利用AJAX实现DropDownList与GridView做实时更新 输出时清除GridView的Style ASP.NET实现打印的方法小结 提高ASP.NET性能的一点方法 asp.net的一些小技巧 route命令详解
获取GridView单元格值的通用函数
赣江源 · 2008-01-09 · via 博客园 - 赣江源

一般要取得 GridView 中的单元格值,都是要指定该单元格所在的行列索引,根据单元格在浏览或编辑模式下,需要使用不同的方式来获取。

例如有一个“地区”的 BoundField,它是 GridView 中的第3列,在浏览模式下取得“地区”列的值,如下:

GridViewRow.Cells(3).Text

如果是在编辑模式时,因为该列值是在 Cell 中的 TextBox中,所以要使用下列方式来提取编辑时“地区”列的值,如下:

CType(oRow.Cells(3).Controls(0), TextBox).Text

以上获取 GridView 单元格值的没有良好的通用性,只要改变列顺序或变更列的类型(例如变成 TemplateField),这样程序很容易就发生错误,如果有变化则程序也需要随时修改。

为了在实际应用中解决这个问题,经过几种方式的对比,感觉比较好的方式是以列名来提取值。DataControlField 有一个 ExtractValuesFromCell 方法,不论是浏览或编辑模式都可以简单的取出 Cell 的对应的列,也不用去管它使用那一种 DataControlField (BoundField 、 CheckBoxField 或 TemplateField ) 都可以正确的取得对应的单元格值。

下面的程序示例就是通过 ExtractRowValues 函数获取出 GridView 指定单元格的值。
C#.NET:

private OrderedDictionary ExtractRowValues(DataControlFieldCollection Columns, GridViewRow Row)
    {
        OrderedDictionary oFieldValues;
        OrderedDictionary oDictionary;
        DataControlField oColumn;
        
        oFieldValues 
= new OrderedDictionary(Columns.Count);
        oDictionary 
= new OrderedDictionary();for (int i = 0; i < Columns.Count; i++)
        {
            oColumn 
= Columns[i];
            
if (oColumn.Visible)
            {
                oDictionary.Clear();
                oColumn.ExtractValuesFromCell(oDictionary, (DataControlFieldCell)Row.Cells[i], Row.RowState, 
true);
                
foreach (DictionaryEntry oEntry in oDictionary)
                {
                    oFieldValues.Add(oEntry.Key, oEntry.Value);
                }
            }
        }
return oFieldValues;
    }
    
    
protected void Button1_Click(Object sender, EventArgs e)
    {
        GridViewRow oRow;
        System.Collections.Specialized.OrderedDictionary oFieldValues;
        oRow 
= (GridViewRow)GridView1.Rows[1];
        oFieldValues 
= ExtractRowValues(GridView1.Columns, oRow);//输出 “地区”名
        this.Response.Write(oFieldValues["地区"].ToString());
    }


VB.NET:

    ''' <summary>
    
''' 获取 GridView 指定列集合。
    
''' </summary>
    
''' <param name="Columns">列集合。</param>
    
''' <param name="Row">行。</param>
    Private Function ExtractRowValues(ByVal Columns As DataControlFieldCollection, ByVal Row As GridViewRow) As OrderedDictionary
        
Dim oFieldValues As OrderedDictionary
        
Dim oColumn As DataControlField
        
Dim oDictionary As OrderedDictionary
        
Dim oEntry As DictionaryEntry
        
Dim N1 As Integer

        oFieldValues 

= New OrderedDictionary(Columns.Count)
        oDictionary 
= New OrderedDictionary()For N1 = 0 To Columns.Count - 1
            oColumn 
= Columns.Item(N1)
            
If oColumn.Visible Then
                oDictionary.Clear()
                oColumn.ExtractValuesFromCell(oDictionary, TryCast(Row.Cells.Item(N1), DataControlFieldCell), Row.RowState, 
True)
                
For Each oEntry In oDictionary
                    oFieldValues.Item(oEntry.Key) 
= oEntry.Value
                
Next
            
End If
        
NextReturn oFieldValues
    
End FunctionProtected Sub Button1_Click(ByVal sender As ObjectByVal e As System.EventArgs)
        
Dim oRow As GridViewRow
        
Dim oFieldValues As OrderedDictionary

        oRow 

= CType(CType(sender, Control).BindingContainer, GridViewRow)
        oFieldValues 
= ExtractRowValues(GridView1.Columns, oRow)'输出 “地区”名
        Me.Response.Write(oFieldValues("地区").ToString())
    
End Sub