如何从 GridView 的模板字段中获取值? [英] How to get values from template fields in GridView?

查看:30
本文介绍了如何从 GridView 的模板字段中获取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我对 GridView 的标记.

This is my markup of GridView.

<Columns>
    <asp:TemplateField HeaderText="Customer Name">
        <ItemTemplate>
            <asp:Label ID="lblname" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Customer.Name")%>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="PickUpPoint">
        <ItemTemplate>
            <asp:Label ID="lblPickUpPoint" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Pickuppoint")%>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

我有一个按钮可以将值存储在 excel 对象的工作表单元格中.

I have a button which stores the values in the worksheet cells of excel object.

for (int i = 0; i < GridView2.Rows.Count; i++)
{
    for (int j = 0; j < GridView2.Rows[i].Cells.Count; j++)
    {
        xlWorkSheet.Cells[i + 1, j + 1] = GridView2.Rows[i].Cells[j].Text;
   }
}

如何获取 GridView 的值并将其存储在工作表中,因为 GridView2.Rows[i].Cells[j].Text 返回空字符串.

How do I get the values of GridView and store it in a worksheet, as the GridView2.Rows[i].Cells[j].Text returns empty string.

推荐答案

您缺少类型转换.这样做-

Your are missing a type cast. Do it like this-

Label name = (Label)GridView2.Rows[i].Cells[j].FindControl("lblname");
xlWorkSheet.Cells[i + 1, j + 1] = name.Text;

更新-如果你可以将你的标签命名为Label0和Label1,那么在第二个for循环中-

Update- If you can name your labels as Label0 and Label1, then in the second for loop-

for (int j = 0; j < GridView2.Rows[i].Cells.Count; j++)
  {
     Label xyz = (Label)GridView2.Rows[i].Cells[j].FindControl("Label"+j);
     xlWorkSheet.Cells[i + 1, j + 1] = xyz.Text;
  }

对于标题文本- string hText = GridView2.HeaderRow.Cells[your column number].Text;

这篇关于如何从 GridView 的模板字段中获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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