【问题标题】:How to get values from template fields in GridView?如何从 GridView 中的模板字段中获取值?
【发布时间】:2012-12-10 05:25:35
【问题描述】:

这是我对 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 对象的工作表单元格中。

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 返回空字符串。

【问题讨论】:

    标签: c# asp.net excel gridview templatefield


    【解决方案1】:

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

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

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

    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;

    【讨论】:

    • 好的...但是,如何为其他控件执行此操作...如果循环进入第二个 tym...我如何将标签指向lblPickUpPoint....谢谢
    • 像个魅力男一样工作......谢谢......工作表也是……???在此先感谢...
    • 我怎样才能让TemplateField HeaderText 也进入工作表...??
    • 嘿,最后一个问题...?? GridView 在 excel 中显示,但格式不正确。我的意思是其中的所有工作表数据都重叠。如何以与 gridview 中显示的方式相同的方式获取 gridview 中的值..?谢谢
    【解决方案2】:

    要检索值,请执行以下操作:

    for (int i = 0; i < GridView2.Rows.Count; i++)
    {
      //extract the TextBox values
      Label lblname= (Label)GridView2.Rows[i].Cells[0].FindControl("lblname");
      Label lblPickUpPoint= (Label)GridView2.Rows[i].Cells[0].FindControl("lblPickUpPoint");
      //Do your excel binding here
    }
    

    【讨论】:

    • 我从字段中获取了值,我应该如何将其绑定到工作表...如xlWorkSheet.Cells[i + 1, j + 1] = GridView2.Rows[i].Cells[j].Text; 我需要做什么,如果我需要按顺序放置它们...编辑: 我需要第二个 for 循环吗..???
    • 如果你想逐行写入excel试试这个:clear-lines.com/blog/post/…如果你想要完整的gridview导出试试这个:codeproject.com/Tips/477436/…
    【解决方案3】:

    {cell}.Text 仅在TemplateField 中没有控件时才有效。您已在模板中添加了一个标签,这就是您首先需要找到控件、将对象强制转换为控件并根据需要访问控件属性的原因。

    如果您想要更通用的方法,您始终可以执行以下操作(删除标签控件并简单地添加评估字段):

    <Columns>
        <asp:TemplateField HeaderText="Customer Name">
            <ItemTemplate>
                <%# DataBinder.Eval(Container.DataItem, "Customer.Name")%>
            </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="PickUpPoint">
             <ItemTemplate>
                 <%# DataBinder.Eval(Container.DataItem, "Pickuppoint")%>
             </ItemTemplate>
         </asp:TemplateField>
    </Columns>
    

    当您使用最初使用的代码时,{cell}.Text 不应再返回空。

    【讨论】:

    • 抱歉,我可以发誓这在某个阶段有效,但我一定是大错特错了。如果您不打算对数据进行任何自定义操作,我认为最好的选择是使用 BoundField。否则,您可以使用((Label)gvTest.Rows[i].Cells[j].Controls[1]).Text 据我所知,假设您没有在标签之前在模板中添加其他控件,则标签控件将始终位于索引 1。
    【解决方案4】:

    尝试使用此代码,我遇到了类似的问题。我认为这段代码更具动态性,您不必每次都找到标签的名称。 但是要保持控件之间的对应关系 以及索引 Controls [ ]:

    for (int row = 1; row <= totalRows; row++)
    {
        for (int col = 0; col < totalCols; col++)
        {
            if (GridView1.Columns[col].Visible)
            {
                if (String.IsNullOrEmpty(GridView1.Rows[row - 1].Cells[col].Text))
                {
                    if (GridView1.Rows[row - 1].Cells[col].Controls[1].GetType().ToString().Contains("Label"))
                    {
                        Label LB = (Label)GridView1.Rows[row - 1].Cells[col].Controls[1];
                        workSheet.Cells[row + 1, col + 1].Value = LB.Text;
                    }
                    else if (GridView1.Rows[row - 1].Cells[col].Controls[1].GetType().ToString().Contains("LinkButton"))
                    {
                        LinkButton LB = (LinkButton)GridView1.Rows[row - 1].Cells[col].Controls[1];
                        workSheet.Cells[row + 1, col + 1].Value = LB.Text;
                    }
                }
                else
                {
                    workSheet.Cells[row + 1, col + 1].Value = GridView1.Rows[row - 1].Cells[col].Text;
                }
    

    【讨论】:

      【解决方案5】:
       protected void Button1_Click(object sender, EventArgs e)
          {
              int i = 0;
              for (i = 0; i <= GvSchedule.Rows.Count - 1; i++)
              {
                  if (((CheckBox)GvSchedule.Rows[i].FindControl("ChkIsService")).Checked)
                  {
                      string catName = ((Label)GvSchedule.Rows[i].FindControl("lblCatName")).Text;
                      var subCatName = ((Label)GvSchedule.Rows[i].FindControl("lblSubCatName")).Text;
                  }
              }
           }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-22
        • 2014-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多