只获得GridView的单元格值知行和列的索引 [英] Get GridView Cell Value Knowing Row And Column Index Only

查看:103
本文介绍了只获得GridView的单元格值知行和列的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的问题的标题是相当简单的。

I think my question title is quite straight forward.

任何帮助表示赞赏。

推荐答案

通过绑定列,并可以使用 GridView1.Rows [X] .Cells [X]。文本但编辑模式下,您有只读模式使用Controls集合得到控制的参考。该方法返回一个控制对象。

With BoundField and in readonly mode you can use GridView1.Rows[x].Cells[x].Text but with edit mode you have to use Controls collection to get reference of a control. This method returns a Control object.

Control control=GridView1.Rows[x].Cells[x].Controls[0]; // later you may cast it to appropriate control class.

如果模板字段使用,那么你必须发出的FindControl 细胞的方法采集获得基于其 ID 对照参考。您也可以使用细胞[X] .Controls 集合了。

If template field is used then you have to issue FindControl method from the Cells collection to get reference of a control based upon its ID. You may also use Cells[x].Controls collection too.

Control control=GridView1.Rows[x].Cells[x].FindControl("ID_Of_Control"); // later you may cast it to appropriate control class.



编辑:

也可以可以有具有在整个模板列相同的名称/标识的一个或多个控制。在这种情况下,你不能使用的FindControl

It is also possible that there can be one or more controls having same name/ID across the Templatefields. In that case you can't use FindControl method.

例如:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="Button1" runat="server" Text="Button" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="Button1" runat="server" Text="Button" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

现在获取按钮,并从第二排和第1单元格更改其文本:

Now to get Button and change its text from 2nd row and 1st cell:

 Button btn = GridView1.Rows[1].Cells[0].Controls[1] as Button ;
 if(btn!=null)
    btn.Text = "Hello";

这篇关于只获得GridView的单元格值知行和列的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆