我正在尝试获取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
是内部网格视图。
答案 0 :(得分:1)
您可以直接从数据源
获取string str = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "YourColumnName"));
要通过索引访问它,您可以尝试
string str = ((DataBoundLiteralControl)e.Row.Cells[x].Controls[y]).Text;
答案 1 :(得分:1)
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string name = e.Row.Cells[0].Text;
}
}