Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取gridview的列值_C#_Asp.net_Gridview - Fatal编程技术网

C# 获取gridview的列值

C# 获取gridview的列值,c#,asp.net,gridview,C#,Asp.net,Gridview,我试图获取gridview中单元格的值。我可以通过这样的标签名称访问它 Label cell1 = ((Label)e.Row.FindControl("cellLabel")); 有15-20列,所以我想通过索引访问单元格。我用e.Row.Cells[2].Text尝试了它,但是我在这里得到了空值 我无法直接访问gridview,因为它是内部gridview。如何访问RowDatabound中的单元格值 示例Gridview <asp:GridView ID="gvCustomers"

我试图获取gridview中单元格的值。我可以通过这样的标签名称访问它

Label cell1 = ((Label)e.Row.FindControl("cellLabel"));
有15-20列,所以我想通过索引访问单元格。我用
e.Row.Cells[2].Text尝试了它,但是我在这里得到了空值

我无法直接访问gridview,因为它是内部gridview。如何访问RowDatabound中的单元格值

示例Gridview

<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" CssClass="Grid"

    DataKeyNames="CustomerID" OnRowDataBound="OnRowDataBound">

    <Columns>

        <asp:TemplateField>

            <ItemTemplate>

                <img alt = "" style="cursor: pointer" src="images/plus.png" />

                <asp:Panel ID="pnlOrders" runat="server" Style="display: none">

                    <asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" CssClass = "ChildGrid">

                        <Columns>

                            <asp:BoundField ItemStyle-Width="150px" DataField="OrderId" HeaderText="Order Id" />

                            <asp:BoundField ItemStyle-Width="150px" DataField="OrderDate" HeaderText="Date" />

                        </Columns>

                    </asp:GridView>

                </asp:Panel>

            </ItemTemplate>

        </asp:TemplateField>

        <asp:BoundField ItemStyle-Width="150px" DataField="ContactName" HeaderText="Contact Name" />

        <asp:BoundField ItemStyle-Width="150px" DataField="City" HeaderText="City" />

    </Columns>

</asp:GridView>


gvOrders
是内部网格视图。

您可以直接从数据源获取它

string str = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "YourColumnName"));
要通过索引访问它,您可以尝试

string str = ((DataBoundLiteralControl)e.Row.Cells[x].Controls[y]).Text;

您可以直接从数据源获取它

string str = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "YourColumnName"));
要通过索引访问它,您可以尝试

string str = ((DataBoundLiteralControl)e.Row.Cells[x].Controls[y]).Text;

显示您的.aspx gridViewbest是使用TemplateField而不是BoundField您正在尝试获取父gridview或嵌套gridview的值?显示您的.aspx gridViewbest是使用TemplateField而不是BoundField您正在尝试获取父gridview或嵌套gridview的值?正如我所说的,我不想使用name,因为有很多,所以我想改用索引。有可能吗?@HarshitShrivastava-但是你为什么要避免使用名字呢?你不认为使用
索引将使你的代码容易出错吗?不,正如我所说,有很多列。所以我可以使用for循环作为索引,因为我想在更多的应用程序中使用它。在以后的程序中使用名称将比在列中更容易实现。@HarshitShrivastava:-更新了我的代码,但使用名称而不是索引将是一种更好的推荐方法。正如我所说的,我不想使用名称,因为有很多,所以我想使用索引。有可能吗?@HarshitShrivastava-但是你为什么要避免使用名字呢?你不认为使用
索引将使你的代码容易出错吗?不,正如我所说,有很多列。所以我可以使用for循环作为索引,因为我想在更多的应用程序中使用它。在以后的程序中使用名称比在列中更容易实现。@HarshitShrivastava:-更新了我的代码,但使用名称而不是索引将是一种更好的推荐方法。