How to get values from template fields in GridView?
Asked Answered
B

5

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.

Boloney answered 10/12, 2012 at 5:25 Comment(0)
S
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;

Spile answered 10/12, 2012 at 5:55 Comment(4)
okay...but, how to do it for other controls...if the loop enters the second tym...how can i point the label to lblPickUpPoint.... thanksBoloney
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...Boloney
How can i make TemplateField HeaderText get inside the worksheet as well...??Boloney
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..?? ThanksBoloney
A
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
}
Aquinas answered 10/12, 2012 at 5:36 Comment(2)
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..???Boloney
If you want to write line by line to excel try this :clear-lines.com/blog/post/… If you want full gridview export try this :codeproject.com/Tips/477436/…Aquinas
E
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.

Equities answered 10/12, 2012 at 6:10 Comment(1)
<facepalm> 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.Equities
F
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;
            }
Ferrocyanide answered 9/8, 2016 at 11:49 Comment(0)
G
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;
            }
        }
     }
Garibull answered 3/8, 2013 at 12:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.