8

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.

Sandeep
  • 278
  • 1
  • 6
  • 21

5 Answers5

11

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;

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

For Header text- string hText = GridView2.HeaderRow.Cells[your column number].Text;

Shiridish
  • 4,942
  • 5
  • 33
  • 64
  • okay...but, how to do it for other controls...if the loop enters the second tym...how can i point the label to `lblPickUpPoint`.... thanks – Sandeep Dec 10 '12 at 06:13
  • works like a charm man....thanks...... Edit: Now i'm getting all the data inside the Gridview.. how can i make the `TemplateField HeaderText` get inside the worksheet as well...??? thanks in advance... – Sandeep Dec 10 '12 at 06:54
  • How can i make `TemplateField HeaderText` get inside the worksheet as well...?? – Sandeep Dec 10 '12 at 07:01
  • hey, 1 last que...?? The GridView is Displayed in excel, but its not proper format. I mean all some of the Sheet's data in them overlap. how can i get the values in the gridview in the same way as it is displayed in the gridview..?? Thanks – Sandeep Dec 10 '12 at 09:45
3

For retrieving values do this:

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
}
sajanyamaha
  • 3,119
  • 2
  • 26
  • 44
  • i got the values from the fields, how should i bind it to the worksheet...as `xlWorkSheet.Cells[i + 1, j + 1] = GridView2.Rows[i].Cells[j].Text;` What do i need to do, if i need to place them in order... edit: Do i need the second for loop..??? – Sandeep Dec 10 '12 at 05:45
  • 1
    If you want to write line by line to excel try this :http://www.clear-lines.com/blog/post/Write-data-to-an-Excel-worksheet-with-C-fast.aspx If you want full gridview export try this :http://www.codeproject.com/Tips/477436/Export-Gridview-Data-to-Excel-in-ASP-NET – sajanyamaha Dec 10 '12 at 06:14
1

{cell}.Text will only work if there isn't a control within the TemplateField. You have added a label to your template which is why you first need to find the control, cast your object to the control and access the control's properties as needed.

If you want a more generic approach you could always do the following (remove the label control and simply add the evaluated field):

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

When you use the code you initially used, the {cell}.Text should no longer return empty.

Clarice Bouwer
  • 3,631
  • 3
  • 32
  • 55
  • 1
    Apologies, I could've sworn that worked at one stage but I must be horribly mistaken. I think the best bet is to use a BoundField if you are not going to do anything custom with your data. Otherwise you could use `((Label)gvTest.Rows[i].Cells[j].Controls[1]).Text` To the best of my knowledge, the label control will always be at index 1 assuming you have no additional controls added in your template prior to the label. – Clarice Bouwer Dec 10 '12 at 06:35
1

Try using this code, I had a similar problem. I think this code is more dynamic and you do not have to find the name of the label every time. But to keep the correspondence between the controls And the index 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;
            }
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;
            }
        }
     }
Abdul Khaliq
  • 2,139
  • 4
  • 27
  • 31