9

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>

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;
   }
}

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.

4

5 回答 5

11

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

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;

于 2012-12-10T05:55:43.733 回答
3

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

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
}
于 2012-12-10T05:36:22.360 回答
1

{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不应再返回空。

于 2012-12-10T06:10:04.180 回答
1

尝试使用此代码,我遇到了类似的问题。我认为这段代码更具动态性,您不必每次都找到标签的名称。但是要保持控件之间的对应关系 和索引 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;
            }
于 2016-08-09T11:49:10.730 回答
0
 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;
            }
        }
     }
于 2013-08-03T12:40:35.020 回答