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

推荐订阅源

博客园 - 三生石上(FineUI控件)
Hugging Face - Blog
Hugging Face - Blog
M
MIT News - Artificial intelligence
T
Tailwind CSS Blog
Webroot Blog
Webroot Blog
S
Secure Thoughts
N
News and Events Feed by Topic
月光博客
月光博客
TaoSecurity Blog
TaoSecurity Blog
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
N
News | PayPal Newsroom
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
小众软件
小众软件
Recent Commits to openclaw:main
Recent Commits to openclaw:main
P
Privacy & Cybersecurity Law Blog
GbyAI
GbyAI
K
Kaspersky official blog
WordPress大学
WordPress大学
P
Proofpoint News Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
博客园 - 叶小钗
W
WeLiveSecurity
Jina AI
Jina AI
The Cloudflare Blog
Project Zero
Project Zero
Simon Willison's Weblog
Simon Willison's Weblog
V
Vulnerabilities – Threatpost
L
LangChain Blog
Forbes - Security
Forbes - Security
PCI Perspectives
PCI Perspectives
Engineering at Meta
Engineering at Meta
Google DeepMind News
Google DeepMind News
Recorded Future
Recorded Future
博客园 - 【当耐特】
H
Heimdal Security Blog
A
About on SuperTechFans
Cisco Talos Blog
Cisco Talos Blog
T
Threat Research - Cisco Blogs
云风的 BLOG
云风的 BLOG
Spread Privacy
Spread Privacy
L
LINUX DO - 最新话题
L
Lohrmann on Cybersecurity
Last Week in AI
Last Week in AI
Google DeepMind News
Google DeepMind News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
I
Intezer
Martin Fowler
Martin Fowler
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint

博客园 - 赣江源

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