c#
  • asp.net
  • excel
  • gridview
  • templatefield
  • 2012-12-10 51 views 6 likes 
    6

    這是我的GridView標記。如何從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]。文本返回空字符串。

    回答

    7

    你缺少一個類型轉換。做到像這個 -

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

    更新 -如果你能說出你的標籤爲Label0和Label1的,然後在第二個爲環 -

    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;

    +0

    好吧......但是,如何做到這一點對於其他控件...如果循環進入第二個tym ...我如何將標籤指向'lblPickUpPoint' .... 謝謝 – Kulkarni

    +0

    作品就像一個魅力男人....謝謝...... 編輯:現在我正在獲取Gridview中的所有數據.. 我怎樣才能使'TemplateField HeaderText'進入工作表內部...... 在此先感謝... – Kulkarni

    +0

    我如何使'TemplateField HeaderText'進入工作表內部以及......? – Kulkarni

    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 
    } 
    
    +0

    我從字段中的值,我應該如何把它綁定到工作表...作爲 'xlWorkSheet.Cells [我+ 1,j + 1] = GridView2.Rows [i] .Cells [j] .Text;' 我需要做什麼,如果我需要將它們按順序排列... 編輯:我需要第二個for循環..? – Kulkarni

    +1

    如果你想逐行寫excel請試試這個:http://www.clear-lines.com/blog/post/Write-data-to-an-Excel-worksheet-with-C-fast.aspx 如果你想完整的gridview導出試試這個:http://www.codeproject.com/Tips/477436/Export-Gridview-Data-to-Excel-in-ASP-NET – sajanyamaha

    0

    {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不應該更長的空回報。

    +0

    道歉,我可以發誓在一個階段工作,但我一定是可怕的錯誤。我認爲最好的選擇是使用BoundField,如果您不打算對數據進行任何自定義操作。否則,你可以使用'((Label)gvTest.Rows [i] .Cells [j] .Controls [1])。Text'盡我所知,標籤控件將始終在索引1處,假設您沒有額外的控件添加到標籤之前的模板中。 – cbillowes

    -1

    使用這種類型 「按鈕btnledName =(按鈕)發件人;」 它-111幫助你 保護無效btnledName_Click(對象發件人,EventArgs的) {

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

    我在我的項目中嘗試了Addbutton的單擊事件.........它工作正常 –

    1

    使用此代碼試試,我有類似的問題。我認爲這個代碼更具動態性,你不必每次都找到標籤的名字。 但保持控制 和索引控制[]之間的對應關係:

    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; 
          } 
    
    相關問題