How to get values โ€‹โ€‹from template fields in GridView?

This is my GridView markup.

<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 that saves the values โ€‹โ€‹in the cells of a sheet 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 to get GridView values โ€‹โ€‹and save them on a worksheet, since GridView2.Rows[i].Cells[j] .Text returns an empty string.

+6
source share
6 answers

You are missing a type. 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 the 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 heading text - string hText = GridView2.HeaderRow.Cells[your column number].Text;

+8
source

To get the values โ€‹โ€‹do the following:

 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 } 
+3
source

Try using this code, I had a similar problem. I think this code is more dynamic and you donโ€™t have to find the label name every time. But in order to maintain consistency between the controls And the Controls [] pointers:

 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; } 
+1
source

{cell}.Text will only work if there is no control in the TemplateField . You added a shortcut to your template, so you first need to find the control, apply your object to the control, and access the control properties as needed.

If you want to use a more general approach, you can always do the following (remove the label control and just add the field to be evaluated):

 <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 originally used code, {cell}.Text no longer return empty.

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

use this type of "Button btnledName = (Button) sender;" This will help you protected void btnledName_Click (object sender, EventArgs e) {

  Button btnledName = (Button)sender; GridViewRow Grow = (GridViewRow)btnledName.NamingContainer; Label lblname = new Label(); Label lblPickUpPoint= new Label(); lblname = (Label)Grow.FindControl("lblname"); lblPickUpPoint = (Label)Grow.FindControl("lblname"); #region if (lblname.Text.Length > 0) { //Add in your work sheet } #endregion } 
-1
source

All Articles